当JSONP没有响应时,IE弹出“语法错误”

时间:2015-07-28 21:39:43

标签: javascript jquery ajax json internet-explorer

当前应用程序必须从另一个应用程序检索信息,并且此其他应用程序不需要处于活动状态以响应JSONP请求,因此启动请求程序将弹出有关它的警报消息。

function jsonRequest(requestURL, errorMsg){
    var err = "";
    var requestData= {param1: value1, param2: value2};
    $.ajax({
        type: 'GET',
        async: true,
        data: requestData,
        jsonpCallback: 'jsonCb',
        url: requestURL,
        timeout: 20000,
        dataType: 'jsonp', /* this trigger the syntax error window by IE*/
        contentType: "application/json; charset=utf-8",
        success: function(data) {
            if(data.hasError != null){
                error = data.error;
                alert(error);
            }else{
                //.... logic to output valid values
            } // closing } is not missing..duh
        },//success
        error:function(x, timeout, m) {
            alert(errorMsg);
        }
    });

    return err;
}

所以有三种可能的情况:

  1. JSONP请求从其他应用程序接收有效数据
  2. JSONP请求从其他应用程序接收空数据
  3. JSONP请求没有响应(其他应用程序未激活)
  4. 到目前为止,在IE上测试之前一直很好。问题是当涉及到scenario 3然后IE弹出其经典Syntax Error,点击“关闭”后,$ .ajax error:{..}中的提醒消息显示

    Message: Syntax error
    Line: 1
    Char: 1
    Code: 0
    URI:.......
    

    SCRIPT1002: Syntax error

    IE调试工具非常蹩脚,所以它不允许我去任何细节。在我逐行检查javascript / jsp代码之后,我找到了问题的原因:

    在场景3中,一旦我在javascript代码中将 dataType: "jsonp"更改为dataType: "json" ,则错误不再弹出,但当然整个ajax请求都会失败。当其他应用程序处于非活动状态时,我无法找到调试工具返回IE的内容。

    我想知道是否有任何有效的方法让IE容忍JSONP或任何方法来调试问题的原因。

2 个答案:

答案 0 :(得分:1)

The fact that IE9 works in any scenario with your code is a testament to the sheer incompetence of the microsoft programmers that created the javascript engine for that dinosaur

/rant - Solution to your problem follows: look for [iFrameSizer][Host page] Added missing iframe ID: iFrameResizer0 (http://phpstack-9420-21004-48731.cloudwaysapps.com/onlinecert/certification/login) iframeResizer.js:97 [iFrameSizer][Host page] IFrame scrolling disabled for iFrameResizer0 iframeResizer.js:97 [iFrameSizer][Host page][init] Sending msg to iframe (iFrameResizer0:8:false:true:32:true:true:null:offset:null:null:0:false:parent) iframeResizer.js:97 [iFrameSizer][Host page][iFrame.onload] Sending msg to iframe (iFrameResizer0:8:false:true:32:true:true:null:offset:null:null:0:false:parent) iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] HTML & body height set to "auto" iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] Enable public methods iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] Added event listener: Animation Start iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] Added event listener: Animation Iteration iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] Added event listener: Animation End iframeResizer.contentWindow.js:62 . [iFrameSizer][iFrameResizer0] Added event listener: Device Orientation Change iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] Added event listener: Transition End iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] Added event listener: Window Clicked iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] Enable MutationObserver iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] In page linking not enabled iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] Trigger event lock on iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] Sending message to host page (iFrameResizer0:150:703:init) iframeResizer.js:97 [iFrameSizer][Host page] Received: [iFrameSizer]iFrameResizer0:150:703:init iframeResizer.js:97 [iFrameSizer][Host page] Checking connection is from: http://phpstack-9420-21004-48731.cloudwaysapps.com iframeResizer.js:97 [iFrameSizer][Host page] Checking height is in range 0-Infinity iframeResizer.js:97 [iFrameSizer][Host page] Checking width is in range 0-Infinity iframeResizer.js:97 [iFrameSizer][Host page] Requesting animation frame iframeResizer.js:97 [iFrameSizer][Host page] IFrame (iFrameResizer0) height set to 150px iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] Trigger event lock off iframeResizer.contentWindow.js:62 [iFrameSizer][iFrameResizer0] --

// you forgot this closing brace

答案 1 :(得分:0)

经过几个小时的拼命调试,最终解决了这个问题:

只需将此设置放在ajax代码中,然后就不会弹出脚本错误:

crossDomain: true,

这样

$.ajax({
    type: 'GET',
    url: aUrl,
    async: true,
    data: urlData,
    jsonpCallback: 'jsoncallback',
    crossDomain: true, /***** the life saver ****/
    timeout: 20000,
    dataType: 'jsonp', 
    contentType: "application/json; charset=utf-8",
    success: function(json) {
        if(json.hasError != null && json.hasError == "true"){
            error = json.error;
            alert(error);
        }else{
            //.... logic to output valid values
        } 

    },//success
    error:function(x, tm, m) {
        alert(timeoutErr);
    }
});

工作得很好