"无效标签"使用JSONP什么时候?什么时候、标签、QUOT、JSONP

2023-09-10 19:16:03 作者:你是恩赐也是劫

我有一个问题,我的JSONP要求.. 将不被显示的数据,萤火虫显示无效的标签错误..

我的javascript:

  $。阿贾克斯({
    网址:链接,
    数据类型:JSONP
    beforeSend:功能(XHR){
        变种的base64 = BTOA(用户名+:+密码);
        xhr.setRequestHeader(授权,基本+ BASE64);
        xhr.overrideMimeType(应用/ JSON);
    },
    jsonpCallback:getResources
})

函数getResources(数据){
    警报(数据);
    警报(JSON.parse(数据));
    $每个(data.groupStatus,功能(I,项目){
        $(机构)追加(< P> ID:+ item.id +< / P>中);
    });
}
 

我的JSON:

  {
    groupStatus:[
        {
            ID:应用程序层配置,ApplicationLayer
            时代:1332755316976,
            级别:0,
            warningIds:[],
            errorIds:[]
        },
        {
            ID:应用程序层ApplicationLayer:nscalealinst2
            时代:1333431531046,
            级别:0,
            warningIds:[],
            errorIds:[]
        }
    ]
}
 

我的HTML:

 < HTML>
< HEAD>
    <冠军>显示器和LT; /标题>
    <链接HREF =CSS / gadget.css相对=样式类型=文本/ CSS/>
    <脚本类型=文/ JavaScript的SRC =JS / jQuery的-1.7.2.js>< / SCRIPT>
< /头>
<身体GT;
    < D​​IV ID =内容>< / DIV>
    <脚本类型=文/ JavaScript的SRC =JS / gadget.js>< / SCRIPT>
< /身体GT;
 
安装打印机时提示数据无效怎么办

请求工作正常,但无论如何是不显示的数据。

林寻找了好几天的解决方案..有人可以帮我吗?预先感谢您!

解决方案:(更新:12年6月9日)

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

  jQuery.support.cors = TRUE;
 

解决方案

到JSONP调用的响应需要包装在一个函数调用,在那里被称为是在URL通常提供的函数名的JSON本身。 jQuery的自动添加的回调到被请求的URL查询字符串参数,所以你的服务器上的脚本应该做同样的事:

  //假设$ JSON包含您的JSON内容
打印{$ _REQUEST ['回调']}({$ JSON});;
 

的原因添加到函数的响应的名称是一个JSONP请求实际上是添加到DOM而不是将由一个XMLHtt prequest对象进行定期请求的脚本标记。使用JSONP允许浏览器进行,否则将阻止适用(默认),以一个XHR跨域策略跨域请求。

I've got a problem with my JSONP request.. The data won't be displayed, Firebug shows an "invalid label" error..

My 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>");
    });
}

My JSON:

{
    "groupStatus": [
        {
            "id": "Application Layer Configuration-ApplicationLayer",
            "time": 1332755316976,
            "level": 0,
            "warningIds": [],
            "errorIds": []
        },
        {
            "id": "Application Layer-ApplicationLayer:nscalealinst2",
            "time": 1333431531046,
            "level": 0,
            "warningIds": [],
            "errorIds": []
        }
    ]
}

My 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>

The request works fine, but anyway the data isn't displayed.

Im searching for a solution for days.. Can somebody help me? Thank you in advance!

SOLUTION (update: 06.09.12)

I've solved this problem. The server (REST interface) on which the was executed had no callback function implemented.. Another way to set up crossdomain requests WITHOUT using JSONP is to set the following jquery variable:

jQuery.support.cors = true;

解决方案

The response to a JSONP call needs to wrap the JSON itself in a function call, where the name of the function being called is usually supplied in the url. jQuery automatically adds a query string parameter of "callback" to the URL that is being requested, so the script on your server should do something similar to:

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

The reason for adding the name of a function to the response is that a JSONP request is actually a script tag appended to the DOM rather than a regular request that would be made by an XMLHttpRequest object. Using JSONP allows the browser to make cross-domain requests that would otherwise be blocked by the cross-domain policy that applies (by default) to an XHR.

 
精彩推荐