简体   繁体   English

如何将字符串数据从ajax成功转换为javascript数据?

[英]How to Convert string data from ajax success to javascript data?

Hello Everybody i have this code and i'm a beginner at ajax things ,and i'm using tinymce jquery and i'm using mention plugin which i found in,so what i'm doing in this code is to make a list of users that i can mention to so i need to make this code run: 大家好,我有这段代码,我是ajax的初学者,我使用的是tinymce jquery,我使用的是我在其中找到的提及插件,所以我在这段代码中所做的就是列出我可以提及的用户,因此我需要运行以下代码:

mention plugin website is: https://github.com/CogniStreamer/tinyMCE-mention 提及插件网站是: https : //github.com/CogniStreamer/tinyMCE-mention

WebMethod Part: WebMethod部分:

 [WebMethod]
    public static string GetUsers()
    {
        return "[{name:'Messi'},{name:'Jason'},{name:'Omar'},{name:'Eren'}]";
    }

and here is the ajax code part: 这是ajax代码部分:

 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: 这是jQuery tinymce选项部分:

 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'},......]
        },})

so really what i need to do is to convert dat() return part to be just like the default and i'm really thankful for helping me 因此,实际上我需要做的就是将dat()返回部分转换为默认值,我真的很感谢您对我的帮助

So your success function is a callback function it's not fired before your dat function completes. 因此,您的成功函数是一个回调函数,在您的dat函数完成之前不会触发它。 So the dat function doesn't return anything. 因此dat函数不返回任何内容。 Try this add asyc: false so the method waits. 尝试添加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;
    }

You should not need this but to answer the title of your question you would use JSON.parse() 您不需要这个,但要回答问题的标题,您将使用JSON.parse()

You could also move the tiny mce init function to execute in the success function and you could then keep the call async. 您还可以移动微小的mce init函数以在成功函数中执行,然后可以保持调用异步。

I found the answer to my question. 我找到了问题的答案。

What i needed is the eval() function. 我需要的是eval()函数。 I replaced source:dat() with source: eval(dat()) and everything works fine. 我用source: eval(dat())替换了source:dat() source: eval(dat()) ,一切正常。

you can try 你可以试试

 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 "";

            }
        });
    }

The data itself is already in the correct format. 数据本身已经是正确的格式。

Since the data is fetched asynchronously you have to call the process method in your callback to pass the data back to the plugin. 由于数据是异步获取的,因此您必须在回调中调用process方法,以将数据传递回插件。 This is explained in more detail here . 这将在此处更详细地说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM