开发者问题收集

使用 JQUERY 的 JSON 请求返回对象错误

2012-06-05
2220

我有一个返回有效 JSON 的 aspx 页面 - 但是当通过 JQUERY 调用时,我可以在 Fiddler 中看到返回了 JSON,但抛出了错误 [Object error]。

   protected void Page_Load(object sender, EventArgs e)
   {
    string json = "{\"name\":\"Joe\"}";
    Response.ClearHeaders();
    Response.ClearContent();
    Response.Clear();
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/json";
    Response.ContentEncoding = Encoding.UTF8;
    Response.Write(json);
    Response.End();
   }

使用此页面的 html 页面位于不同的域中,我正在使用 jsonp。

function jsonpCallback(response){
    alert(response.data);
}

$(document).ready(function(){ 

            $.ajax({
                url: 'http://localhost:30413/getprice.aspx',
                dataType: 'jsonp',
                error: function(xhr, status, error) {
                    alert(error);
                },
                success: jsonpCallback
            });

}); 

当请求 aspx 页面时,有效的 JSON 会返回到浏览器,当进行 JQUERY 调用时,会返回 JSON,但不会调用回调函数,并且会出现 Expected ";" JS 错误,然后会显示 [Object error] 消息。以下是请求和响应。

我尝试了请求的每个变体,结果相同。我使用下面的请求 JQUERY 示例,因为它在下面显示的最后一个示例中有效。

GET http://localhost:30413/price.aspx?callback=jQuery17105556924406763212_1338876162569&_=1338876162581 HTTP/1.1
Accept: application/javascript, */*;q=0.8
Referer: http://alpha.tigerdirect.com/applications/b2b/varinfo.asp
Accept-Language: en-US
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)
Accept-Encoding: gzip, deflate
Host: localhost:30413
Connection: Keep-Alive
Pragma: no-cache
Cookie: ASP.NET_SessionId=d4pje2hgm2beznslfpp4pii5



HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Tue, 05 Jun 2012 06:02:42 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Content-Type: application/json; charset=utf-8
Content-Length: 14
Connection: Close

{"name":"Joe"}

此示例有效

function jsonpCallback(response){
    alert(response.data);
}
$(document).ready(function(){ 
        $.ajax({
            url: 'http://api.bitbucket.org/1.0/repositories/retroviz/webformsthemeswitcher/src/tip/.hgignore',
            dataType: 'jsonp',
            error: function(xhr, status, error) {
                alert(error);
            },
            success: jsonpCallback
        });
}); 
2个回答

将 ajax 中的 url 更改为 url: http://localhost:30413/getprice.aspx?callback=?

并且

if(Request.QueryString['callback']!=null){
    string callback = Request.QueryString['callback'];
    string json = "{\"name\":\"Joe\"}";
    Response.ClearHeaders();
    Response.ClearContent();
    Response.Clear();
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/json";
    Response.ContentEncoding = Encoding.UTF8;
    Response.Write(callback + "(" + json + ")");
    Response.End();
}

在 jsop 中,我们将响应包装在 callback 函数调用中。就像我们在 javascript 中调用函数一样。

Jashwant
2012-06-05

JSONP 响应必须包装在带有回调的请求中指定的函数调用中。

类似于

callback({"name":"Joe"});

这通常应在过滤器中处理。

Subir Kumar Sao
2012-06-05