使用JSONP时"无效标签"?

P4t*_*4tR 5 javascript ajax json jsonp syntax-error

我的JSONP请求有问题..数据不会显示,Firebug显示"无效标签"错误.

在此输入图像描述

我的JavaScript:

$.ajax({
    url: link,
    dataType: "jsonp",
    beforeSend: function(xhr) {
        var base64 = btoa(username + ":" + password);
        xhr.setRequestHeader("Authorization", "Basic" + base64);
        xhr.overrideMimeType("application/json");
    },
    jsonpCallback: "getResources"
})

function getResources(data) {
    alert(data);
    alert(JSON.parse(data));
    $.each(data.groupStatus, function(i, item) {
        $("body").append("<p>ID: " + item.id + "</p>");
    });
}
Run Code Online (Sandbox Code Playgroud)

我的JSON:

{
    "groupStatus": [
        {
            "id": "Application Layer Configuration-ApplicationLayer",
            "time": 1332755316976,
            "level": 0,
            "warningIds": [],
            "errorIds": []
        },
        {
            "id": "Application Layer-ApplicationLayer:nscalealinst2",
            "time": 1333431531046,
            "level": 0,
            "warningIds": [],
            "errorIds": []
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

我的HTML:

<html>
<head>
    <title>Monitor</title>
    <link href="css/gadget.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="js/jquery-1.7.2.js"></script>
</head>
<body>
    <div id="content"></div>
    <script type="text/javascript" src="js/gadget.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)

请求工作正常,但无论如何数据都不会显示.

在此输入图像描述

我正在寻找解决方案好几天..有人能帮帮我吗?先感谢您!

解决方案(更新:06.09.12)

我已经解决了这个问题.执行了它的服务器(REST接口)没有实现回调函数.设置跨域请求而不使用JSONP的另一种方法是设置以下jquery变量:

jQuery.support.cors = true;
Run Code Online (Sandbox Code Playgroud)

ste*_*ukx 7

对JSONP调用的响应需要在函数调用中包装JSON本身,其中被调用的函数的名称通常在url中提供.jQuery会自动将"callback"的查询字符串参数添加到正在请求的URL中,因此服务器上的脚本应该执行类似于以下操作的操作:

// assuming that $JSON contains your JSON content
print "{$_REQUEST['callback']}( {$JSON} );";
Run Code Online (Sandbox Code Playgroud)

将函数名称添加到响应的原因是JSONP请求实际上是附加到DOM的脚本标记,而不是由XMLHttpRequest对象生成的常规请求.使用JSONP允许浏览器发出跨域请求,否则这些请求将被应用(默认情况下)XHR的跨域策略阻止.