简体   繁体   English

JavaScript ajax,JSONP,回调函数可对数据进行处理

[英]JavaScript ajax, JSONP, Callback function to do something with data

Fellas, 伙计们,

I'm trying to do an ajax call to get some JSON. 我正在尝试进行ajax调用以获取一些JSON。 I can get around the cross origin issues in Chrome very easily but in IE the only way I got it working without throwing an error is using JSONP. 我可以很轻松地解决Chrome中的跨源问题,但是在IE中,我能正常运行而又不会抛出错误的唯一方法是使用JSONP。 My problem is that i don't know how to process the response. 我的问题是我不知道如何处理回复。 I can see that the call executes and it returns JSON in fiddler but how do i catch it and play with it in my code that's below. 我可以看到该调用已执行,并且在fiddler中返回JSON,但是如何在下面的代码中捕获并使用它。

$.support.cors = true;

            function callAjaxIE(){

            $.ajax({                    
                type: 'GET',            
                async: true,
                url: usageUrl,
                dataType: 'jsonp',
                crossDomain: true,
                jsonp: false,
                jsonpCallback: processDataIE,                       
            //  cache: false,
                success: function (data) {
                    alert('success');
                    //processData(data)
                },
                error: function (e){

                    console.log(e);
                }

             }).done(function (data) {                      
                    alert('done');              
             });

function processDataIE(){

alert('processing data ie');
}

It works when i run it, it displays a message box saying 'processing data ie' but so what. 当我运行它时,它可以工作,它显示一个消息框,显示“正在处理数据”,但那又如何。 How do i play with the results? 如何处理结果?

UPDATE: 更新:

So I've updated the code as per Quentin. 因此,我根据Quentin更新了代码。 It doesn't go into the 'success' block. 它不会进入“成功”块。 It errors out with the following. 出现以下错误。

在此处输入图片说明

When i look at fiddler. 当我看着提琴手。 The JSON is there. JSON在那里。 How do i get it? 我如何获得?

在此处输入图片说明

  1. Don't force the function name. 不要强行使用函数名称。 That's a recipe for race conditions. 那是比赛条件的秘诀。 Remove jsonpCallback: processDataIE . 删除jsonpCallback: processDataIE Let jQuery determine the function name. 让jQuery确定函数名称。
  2. Don't remove the callback parameter from the URL. 不要从URL中删除回调参数。 Make the server read callback from the query string to determine which function to call. 使服务器从查询字符串中读取callback ,以确定要调用的函数。 Remove jsonp: false, . 删除jsonp: false,
  3. Use the success function as normal. 正常使用success功能。 (Get rid of processDataIE , that's what success is for). (摆脱processDataIE ,这就是success原因)。

Asides: 旁白:

crossDomain: true is pointless. crossDomain: true是没有意义的。 It tells jQuery that when it is using XHR (which you aren't) and the URL is pointing to the same origin (which it isn't) then it should not add extra headers in case the server does an HTTP redirect to a different origin. 它告诉jQuery,当它使用XHR(不是)并且URL指向相同的来源(不是)时,则不应添加额外的标头,以防服务器将HTTP重定向到另一个起源。

async: true is pointless. async: true是没有意义的。 That's the default, and JSONP requests can't be anything other than async anyway. 这是默认设置,而且JSONP请求只能是异步。

$.support.cors = true; is pointless at best and can be harmful. 充其量是毫无意义的,可能有害。 Remove it. 去掉它。

Don't override jQuery's detection of support for CORS by the browser. 不要覆盖jQuery对浏览器对CORS支持的检测。 You can't make ancient browsers support CORS by lying to jQuery. 您不能通过撒谎来使古老的浏览器支持CORS。 You're using JSONP anyway, so CORS is irrelevant. 无论如何,您都在使用JSONP,因此CORS无关紧要。


The above is the sensible, standard, robust solution. 以上是明智,标准,可靠的解决方案。

If you want the quick hack that maintains all the bad practises you have in play, then just look at the first argument to processDataIE . 如果您想要一种快速的技巧来维持您在使用中的所有不良做法,那么只需查看processDataIE的第一个参数即可。

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

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