我正在使用crossrider实现一个跨浏览器插件。
我有两个连续的AJAX请求(JSON和JSONP):- 第一个是"login"的JSON请求,它设置了一个cookie浏览器。
- 第二个是JSONP请求"保存",它使用cookie将一些数据保存到服务器。
下面是一个简化的代码示例:
$.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:nn" + JSON.stringify(response, undefined));
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("Saving Failure:nn"
+ JSON.stringify(jqXHR, undefined) + "nn"
+ JSON.stringify(textStatus, undefined) + "nn"
+ JSON.stringify(errorThrown, undefined));
});
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("Login Failure:nn"
+ JSON.stringify(jqXHR, undefined) + "nn"
+ JSON.stringify(textStatus, undefined) + "nn"
+ JSON.stringify(errorThrown, undefined));
});
它在IE, FF &Chrome,如果它在一个HTML(自动登录然后自动保存)。
但是当放置在crossrider扩展中时。json(在appAPI.ready中)中,它在每个浏览器中给出三种不同的行为。
For Chrome:
- 登录成功。
{"判别":4,"状态":200年,"statusText":"成功"}
"parseerror"
{}
Firefox:
- 登录成功。
- 弹出请求凭证(好像登录时没有设置cookie !!)
- 后输入凭据(abashir &P@ssw0rd)
- 保存失败,输出:
{"判别":4,"状态":200年,"statusText":"成功"}
"parseerror"
{}
For IE9:
- 登录失败,输出:
{"判别":0,"setRequestHeader":{},…,"statusText":"No Transport"}
"error"
"No Transport"
PS,使用fiddler,我注意到,在chrome浏览器从服务器返回的响应是正确的(尽管ajax失败函数被调用),这是来自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'nnSaved new test as id#558","project_id":"151"});
您可能注意到,生成的回调函数的调用包含正确的json对象,尽管失败函数被回调!并且无法访问此响应以从中提取数据!
如何使前2个连续的请求工作在3个浏览器(IE, FF, Chrome)使用crossrider
我怀疑这个问题与使用$时遇到的跨域限制有关。
你可以通过使用下面示例代码中的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:nn" + appAPI.JSON.stringify(response));
},
onFailure: function (httpCode) {
alert("Saving Failure:nn" + httpCode);
},
postData : {
"pbehave" : "For+the+url+http%3A%2F%2Fstackoverflow.com%2F%0D%0A",
"title" : "test",
"description" : " "
}
});
},
onFailure: function (httpCode) {
alert("Login Failure:nn" + httpCode);
},
postData: {
login_name : "abashir",
password : "P@ssw0rd"
}
});
});