0

我已将 phonegap 项目复制到类固醇,但现在我的 ajax 命令无法工作,我完全不知道是什么原因造成的,所以任何建议都可能会有所帮助。

这是导致问题的代码,它总是在 request.status = 0 时终止错误:

$.ajax({
        type: "POST",
        url: serverUrl + "login.ajax",
        data: { jsonLogin: JSON.stringify(loginObject), deviceInfo: JSON.stringify(deviceInfo)},
        async: true,
        timeout: 7000,
        cache: false,
        headers: { "cache-control": "no-cache" },
        success: function(data) {
                    ...   
                 },
        error: function(request, status, err) {
            ...
        },
        complete: function(){...}
    });     
4

2 回答 2

1

AppGyver 员工在这里!

config/application.coffee,是你的steroids.config.location = "http://localhost/index.html"还是刚刚的index.html?Steroids 通过 localhost(即手机上的内部 Web 服务器)提供应用程序文件,而 PhoneGap 使用 File 协议。使用 localhost 会使 WebView 强制执行更严格的 CORS 规则,因此您需要Access-Control-Allow-Origin服务器响应的标头。通过 File 协议提供的 HTML 文件允许跨域请求在没有 CORS 标头的情况下通过。

您可以在https://github.com/appgyver/steroids-runtime-tests找到一个带有 AJAX 测试的测试项目

于 2013-12-19T08:51:41.437 回答
0

我认为 jQuery 不解析 json 数据,因为你没有指定dataType

 $.ajax({
            type: "POST",
            url: serverUrl + "login.ajax",
            data: { jsonLogin: JSON.stringify(loginObject), deviceInfo: JSON.stringify(deviceInfo)},
            dataType: json, //Specify data type
            async: true,
            timeout: 7000,
            cache: false,
            headers: { "cache-control": "no-cache" },
            success: function(data) {
                        ...   
                     },
            error: function(request, status, err) {
                ...
            },
            complete: function(){...}
        });  
于 2013-12-16T15:55:48.713 回答