AJAX命令在appgyver类固醇

AJAX command in appgyver steroids

本文关键字:类固醇 appgyver 命令 AJAX      更新时间:2023-09-26

我已经将我的phonegap项目复制到了类固醇中,但现在我的ajax命令无法工作,我完全不知道是什么原因导致了它,所以任何建议都可能有用。

这是导致问题的代码,它总是在请求时出错。状态=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(){...}
    });     

AppGyver员工在这里!

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

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

我认为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(){...}
        });