如何将字符串数据从 ajax 成功转换为 javascript 数据?
How to Convert string data from ajax success to javascript data?
大家好,我有这段代码,我是 ajax 方面的初学者,我正在使用 tinymce jquery,我正在使用我在其中找到的提及插件,所以呢我在这段代码中做的是列出我可以提及的用户,所以我需要制作这段代码 运行:
提到插件网站是:
https://github.com/CogniStreamer/tinyMCE-mention
WebMethod Part:
[WebMethod]
public static string GetUsers()
{
return "[{name:'Messi'},{name:'Jason'},{name:'Omar'},{name:'Eren'}]";
}
and here is the ajax code part:
function dat() {
$.ajax({
type: "POST",
url: "Default.aspx/GetUsers",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);//show me this alert "[{name:'Messi'},{name:'Jason'},{name:'Omar'},{name:'Eren'}]"
return data.d;
},
error: function (data) {
return "";
}
});
}
and here is the jquery tinymce options part:
tinymce.init({
selector: '#Selector',
theme: 'modern',
elements: "rte",
plugins: [
'advlist autolink mention lists link image charmap preview hr anchor pagebreak',
'searchreplace wordcount visualblocks visualchars code fullscreen',
'insertdatetime media nonbreaking table contextmenu directionality',
'emoticons template paste textcolor colorpicker imagetools'
],
mentions: {
source:dat()//the default option was [{ name: 'Messi'},{name:'Jason'},......]
},})
所以我真正需要做的是将 dat() return 部分转换为默认值
我真的很感谢你帮助我
所以你的成功函数是一个回调函数,它不会在你的 dat 函数完成之前触发。所以 dat 函数没有 return 任何东西。试试这个 add asyc: false 这样方法就会等待。
function dat() {
var retdat;
$.ajax({
type: "POST",
url: "Default.aspx/GetUsers",
data: "{}",
contentType: "application/json; charset=utf-8"
async:false
dataType: "json",
success: function (data) {
alert(data.d);//show me this alert "[{name:'Messi'},{name:'Jason'},{name:'Omar'},{name:'Eren'}]"
retdat = data.d;
},
error: function (data) {
return "";
}
});
return retdat;
}
你不需要这个,但要回答你的问题标题你会使用 JSON.parse()
您还可以将微型 mce init 函数移动到成功函数中执行,然后您可以保持调用异步。
我找到了问题的答案。
我需要的是 eval()
函数。我用 source: eval(dat())
替换了 source:dat()
,一切正常。
你可以试试
function dat() {
$.ajax({
type: "POST",
url: "Default.aspx/GetUsers",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);//show me this alert "[{name:'Messi'},{name:'Jason'},{name:'Omar'},{name:'Eren'}]"
var result = JSON.parse(data.d);
//or you can do without do statically like split(data.d,'"'); then whatever result you get can return that is also
return result;
},
error: function (data) {
return "";
}
});
}
数据本身的格式已经正确。
由于数据是异步获取的,因此您必须在回调中调用 process
方法将数据传回插件。对此有更详细的解释 here.
大家好,我有这段代码,我是 ajax 方面的初学者,我正在使用 tinymce jquery,我正在使用我在其中找到的提及插件,所以呢我在这段代码中做的是列出我可以提及的用户,所以我需要制作这段代码 运行:
提到插件网站是: https://github.com/CogniStreamer/tinyMCE-mention
WebMethod Part:
[WebMethod]
public static string GetUsers()
{
return "[{name:'Messi'},{name:'Jason'},{name:'Omar'},{name:'Eren'}]";
}
and here is the ajax code part:
function dat() {
$.ajax({
type: "POST",
url: "Default.aspx/GetUsers",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);//show me this alert "[{name:'Messi'},{name:'Jason'},{name:'Omar'},{name:'Eren'}]"
return data.d;
},
error: function (data) {
return "";
}
});
}
and here is the jquery tinymce options part:
tinymce.init({
selector: '#Selector',
theme: 'modern',
elements: "rte",
plugins: [
'advlist autolink mention lists link image charmap preview hr anchor pagebreak',
'searchreplace wordcount visualblocks visualchars code fullscreen',
'insertdatetime media nonbreaking table contextmenu directionality',
'emoticons template paste textcolor colorpicker imagetools'
],
mentions: {
source:dat()//the default option was [{ name: 'Messi'},{name:'Jason'},......]
},})
所以我真正需要做的是将 dat() return 部分转换为默认值 我真的很感谢你帮助我
所以你的成功函数是一个回调函数,它不会在你的 dat 函数完成之前触发。所以 dat 函数没有 return 任何东西。试试这个 add asyc: false 这样方法就会等待。
function dat() {
var retdat;
$.ajax({
type: "POST",
url: "Default.aspx/GetUsers",
data: "{}",
contentType: "application/json; charset=utf-8"
async:false
dataType: "json",
success: function (data) {
alert(data.d);//show me this alert "[{name:'Messi'},{name:'Jason'},{name:'Omar'},{name:'Eren'}]"
retdat = data.d;
},
error: function (data) {
return "";
}
});
return retdat;
}
你不需要这个,但要回答你的问题标题你会使用 JSON.parse()
您还可以将微型 mce init 函数移动到成功函数中执行,然后您可以保持调用异步。
我找到了问题的答案。
我需要的是 eval()
函数。我用 source: eval(dat())
替换了 source:dat()
,一切正常。
你可以试试
function dat() {
$.ajax({
type: "POST",
url: "Default.aspx/GetUsers",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.d);//show me this alert "[{name:'Messi'},{name:'Jason'},{name:'Omar'},{name:'Eren'}]"
var result = JSON.parse(data.d);
//or you can do without do statically like split(data.d,'"'); then whatever result you get can return that is also
return result;
},
error: function (data) {
return "";
}
});
}
数据本身的格式已经正确。
由于数据是异步获取的,因此您必须在回调中调用 process
方法将数据传回插件。对此有更详细的解释 here.