[英]Successive json & jsonp requests fail using Crossrider?
我正在使用crossrider來實現跨瀏覽器插件。
我有兩個連續的AJAX請求(JSON和JSONP):
這是代碼的簡化示例:
$.ajax({
url : "https://app.testafy.com/api/v0/user/login",
type : 'GET',
cache : false,
dataType : 'json',
data : {login_name: "abashir", password: "P@ssw0rd"}
}).done(function(response) {
alert("Login Success");
$.ajax({
url : 'https://app.testafy.com/api/v0/test/save',
type : 'GET',
cache : false,
dataType : 'jsonp',
data : {"pbehave":"For+the+url+http%3A%2F%2Fstackoverflow.com%2F%0D%0A","title":"test","description":" "}
}).done(function(response) {
alert("Saving Success:\n\n" + JSON.stringify(response, undefined));
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("Saving Failure:\n\n"
+ JSON.stringify(jqXHR, undefined) + "\n\n"
+ JSON.stringify(textStatus, undefined) + "\n\n"
+ JSON.stringify(errorThrown, undefined));
});
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("Login Failure:\n\n"
+ JSON.stringify(jqXHR, undefined) + "\n\n"
+ JSON.stringify(textStatus, undefined) + "\n\n"
+ JSON.stringify(errorThrown, undefined));
});
如果以HTML格式(自動登錄然后自動保存),那么它對於IE,FF和Chrome來說效果很好。
但是,當放置在crossrider extension.json(在appAPI.ready中)中時,它會在每種瀏覽器中提供三種不同的行為。
對於Chrome:
{“ readyState”:4,“狀態”:200,“ statusText”:“成功”}
“解析錯誤”
{}
對於Firefox:
{“ readyState”:4,“狀態”:200,“ statusText”:“成功”}
“解析錯誤”
{}
對於IE9:
{“ readyState”:0,“ setRequestHeader”:{},....,“ statusText”:“無傳輸”}
“錯誤”
“禁止運輸”
PS,使用fiddler,我注意到在chrome中,從服務器返回的響應是正確的(盡管調用了ajax fail函數),這是來自fiddler的請求/響應對:
請求:
GET https://app.testafy.com/api/v0/test/save?callback=jQuery17107044411341194063_1364461851960&pbehave=For%2Bthe%2Burl%2Bhttp%253A%252F%252Fstackoverflow.com%252F%250D%250A&title=test&description=+&_=1364461865618 HTTP/1.1
Host: app.testafy.com
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
Referer: http://stackoverflow.com/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: __utma=124963983.2014233417.1360749029.1362583015.1362996135.12; __utmc=124963983; __utmz=124963983.1362322761.9.3.utmcsr=stackoverflow.com|utmccn=(referral)|utmcmd=referral|utmcct=/; GSG_SESSIONID=ee03a02cdb6f3c0e3d812795be63c788
響應:
HTTP/1.1 200 nginx
Access-Control-Allow-Origin: *
Content-Type: application/json; charset=utf-8
Date: Thu, 28 Mar 2013 09:03:48 GMT
P3P: CP="ALL ADM DEV PSAi COM OUR OTRo STP IND ONL"
Set-Cookie: GSG_SESSIONID=ee03a02cdb6f3c0e3d812795be63c788; domain=app.testafy.com; path=/; secure; HttpOnly
Content-Length: 273
Connection: keep-alive
jQuery17107044411341194063_1364461851960({"test_id":"558","message":"Phrase check has found the following error(s), but your test was still saved:\nThere's no For rule for '+the+url+http%3A%2F%2Fstackoverflow.com%2F%0D%0A'\n\nSaved new test as id#558","project_id":"151"});
您可能已經注意到,盡管失敗函數被回調了,但生成的回調函數的調用仍包含正確的json對象。 而且無法訪問此響應以從中提取數據!
如何使用crossrider使前兩個連續請求在3個瀏覽器(IE,FF,Chrome)上工作?
我懷疑該問題與在擴展范圍內使用$ .ajax時遇到的跨域限制有關。
您可以通過以下示例代碼使用我們的appAPI.request.post方法來避免此問題。 該代碼已在Chrome中進行了測試,登錄和保存均成功。
讓我知道您是否需要任何進一步的幫助。
appAPI.ready(function ($) {
appAPI.request.post({
url : "https://app.testafy.com/api/v0/user/login",
onSuccess: function (response) {
alert("Login Success");
appAPI.request.post({
url : 'https://app.testafy.com/api/v0/test/save',
onSuccess: function (response) {
alert("Saving Success:\n\n" + appAPI.JSON.stringify(response));
},
onFailure: function (httpCode) {
alert("Saving Failure:\n\n" + httpCode);
},
postData : {
"pbehave" : "For+the+url+http%3A%2F%2Fstackoverflow.com%2F%0D%0A",
"title" : "test",
"description" : " "
}
});
},
onFailure: function (httpCode) {
alert("Login Failure:\n\n" + httpCode);
},
postData: {
login_name : "abashir",
password : "P@ssw0rd"
}
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.