JSON请求使用JQUERY返回对象错误 [英] JSON request returns object error using JQUERY

查看:76
本文介绍了JSON请求使用JQUERY返回对象错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an aspx page that returns valid JSON - however when called via JQUERY I can see in Fiddler that the JSON is returned but an error is thrown [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.

The html page consuming this page is in a different domain, and I am using 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错误,然后显示[对象错误]消息.以下是请求和响应.

When the aspx page is requested valid JSON is returned to the browser, when the JQUERY call is made JSON is returned but the callback function is not called and an Expected ";" JS error then an [Object error] message is displayed. Below are the request and responses made.

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

I've tried every variation of the request with the same result. I am using the request JQUERY sample below because it works in the last sample shown below.

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

推荐答案

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

Change url in ajax to 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中调用函数一样.

In jsop, we wrap the response in callback function call. Just like we call a function in javascript.

这篇关于JSON请求使用JQUERY返回对象错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆