简体   繁体   English

appgyver类固醇中的AJAX命令

[英]AJAX command in appgyver steroids

I have copied my phonegap project to steroids but now my ajax commands fail to work, I have no clue at all what is causing it so any suggestion might be helpfull. 我已经将phonegap项目复制到了类固醇,但是现在我的ajax命令无法正常工作,我完全不知道是什么原因造成的,因此任何建议都可能会有所帮助。

This is the code that causes the problem, it always terinates to error with request.status = 0: 这是导致问题的代码,它始终会因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(){...}
    });     

AppGyver employee here! AppGyver员工在这里!

In config/application.coffee , is your steroids.config.location = "http://localhost/index.html" or just index.html ? config/application.coffee ,是您的steroids.config.location = "http://localhost/index.html"还是只是index.html Steroids serves app files via localhost (ie an internal web server on the phone) whereas PhoneGap uses the File protocol. 类固醇通过本地主机(即电话上的内部Web服务器)提供应用程序文件,而PhoneGap使用文件协议。 Using localhost makes the WebView enforce stricter CORS rules, so you need an Access-Control-Allow-Origin header to the server response. 使用localhost使WebView强制执行更严格的CORS规则,因此您需要对服务器响应的Access-Control-Allow-Origin标头。 HTML files served via the File protocol let cross-domain requests go through without CORS headers. 通过File协议提供的HTML文件允许跨域请求通过而没有CORS标头。

You can find a test project with an AJAX test at https://github.com/appgyver/steroids-runtime-tests 您可以在以下网址找到带有AJAX测试的测试项目: https://github.com/appgyver/steroids-runtime-tests

I think jQuery does not parse the json data becaus eyou have not specified the dataType 我认为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(){...}
        });  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM