0

我正在使用 crossrider 来实现跨浏览器插件。

我有两个连续的 AJAX 请求(JSON 和 JSONP):

  1. 第一个是“登录”的 JSON 请求,它在浏览器中设置一个 cookie。
  2. 第二个是“保存”的 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:\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)); 
}); 

它适用于 IE、FF 和 Chrome,如果它在 HTML 中(自动登录然后自动保存)。

但是当放置在 crossrider extension.json 中(在 appAPI.ready 中)时,它会在每个浏览器中提供三种不同的行为。

对于铬:

  • 登录成功终止。
  • 保存失败并输出:

{"readyState":4,"status":200,"statusText":"success"}
"parseerror"
{}


对于火狐:

  • 登录成功终止。
  • 弹出窗口要求提供凭据(好像 cookie 不是通过登录设置的!!)
  • 输入凭据后 (abashir & P@ssw0rd)
  • 保存失败并输出:

{"readyState":4,"status":200,"statusText":"success"}
"parseerror"
{}


对于 IE9:

  • 登录失败并输出:

{"readyState":0, "setRequestHeader":{},....,"statusText":"No Transport"}
"error"
"No Transport"


PS,使用提琴手,我注意到在chrome中从服务器返回的响应是正确的(尽管调用了ajax失败函数),这是来自提琴手的请求/响应对:


要求:

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 使之前的 2 个连续请求在 3 个浏览器(IE、FF、Chrome)上工作?

4

1 回答 1

3

我怀疑这个问题与在扩展范围内使用 $.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"
        }
    });
});
于 2013-04-03T09:11:46.767 回答