简体   繁体   English

无法使用CORS发送Phonegap FormData

[英]Phonegap FormData not being sent using CORS

I have a phonegap app (currently using phonegap 3.5) and I set up a CORS request thus: 我有一个phonegap应用程序(当前使用phonegap 3.5),因此我设置了CORS请求:

function createCORSRequest(method, url, asynchronous) {
    var xhr = new XMLHttpRequest();
    //var n = currentTime.getTime();
    var n = 'nocache=' + generateRandomString(10);
    if (url.indexOf('?') == -1) {
        n = '?' + n;
    }
    else {
        n = '&' + n;
    }
    if ("withCredentials" in xhr) {

        // Check if the XMLHttpRequest object has a "withCredentials" property.
        // "withCredentials" only exists on XMLHTTPRequest2 objects.
        xhr.open(method, url + n, asynchronous);
        //xhr.setRequestHeader("pragma", "no-cache");
        //xhr.setRequestHeader('cache-control', 'no-cache');
    } else if (typeof XDomainRequest != "undefined") {
        // Otherwise, check if XDomainRequest.
        // XDomainRequest only exists in IE, and is IE's way of making CORS requests.
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {

        // Otherwise, CORS is not supported by the browser.
        xhr = null;
    }
    return xhr;
}

I then create a request object: 然后,我创建一个请求对象:

var url = base_url + 'login.php';
var xhrLogin = createCORSRequest('POST', url, false);
var formData = new FormData();
formData.append('loginemail','a@b.com');
formData.append('pwd','mypassword');
xhrLogin.send(formData);

On the server, I have the following PHP: 在服务器上,我有以下PHP:

$postdata = '';

foreach($_POST as $key => $value)
{
    $postdata .= ' ' . $key . '=' . $value . ';';
}
if (strlen($postdata) == 0) {
    sendResponse(200, 'Error: No post data ' . $postdata, 'text/html');
    return;
}

When I run this code in a browser, straight off the disk (or from Brackets) it works OK no matter what my base_url variable is pointing to. 当我在浏览器中运行此代码时,无论我的base_url变量指向什么,直接从磁盘(或从Brackets)运行都可以。 If I run this via the phonegap server (either in a browser or on a device using the phonegap developer app) the form data is posted OK when I target my test server but on the production server it reports that no POST variables exist. 如果我通过phonegap服务器(在浏览器中或使用phonegap开发人员应用程序的设备上)运行此服务器,则在以测试服务器为目标时,表单数据将发布为OK,但在生产服务器上,它将报告不存在POST变量。 In each case I view the network tab in chrome (F12) and verify that the username and password are being sent. 在每种情况下,我都使用Chrome浏览器(F12)查看网络标签,并验证是否已发送用户名和密码。

If I build the app and run on either Android or iOS it works fine on either server. 如果我构建该应用程序并在Android或iOS上运行,则可以在任一服务器上正常运行。 It just appears to go awry running from phonegap server and when the url is pointing at the production server. 当URL指向生产服务器时,它似乎无法从phonegap服务器运行。 (I also tried in IE too as the createCorsRequest code is slightly different for that, the same thing happens). (我也曾在IE中尝试过,因为createCorsRequest代码与此稍有不同,发生了相同的事情)。

As you can see from the first block of code, I have tried the nocache tricks from other posts on this forum with similar errors but to no avail. 从第一段代码中可以看到,我已经尝试了该论坛其他帖子的nocache技巧,但出现了类似的错误,但无济于事。

Test server is running php 5.5.9-1ubuntu4.3 Prod server is running php 5.4.20 测试服务器正在运行php 5.5.9-1ubuntu4.3产品服务器正在运行php 5.4.20

Could the version of php be affecting this? php的版本会影响到这个吗?

Update: I tried this from home and it sent data to both servers, it could be a proxy issue. 更新:我在家中尝试过,它向两个服务器发送了数据,这可能是代理问题。

Update2: Running phonegap serve from a Mac seems to work OK (I was running it on a Linux box) Update2:从Mac运行phonegap服务似乎可以正常工作(我在Linux机器上运行它)


It would appear that this is down to the version, I had 3.5.0-0.21.14 installed at work and 3.5.0-0.20.4 at home. 看来这是该版本的问题,我在工作中安装了3.5.0-0.21.14,在家里安装了3.5.0-0.20.4。 As the Mac worked at work I took my work laptop home and tested and it exhibited the same problems. 当Mac在工作时,我将工作用的笔记本电脑带回家并进行了测试,结果显示出同样的问题。

An interesting thing is that if I run the app from a Ripple Emulator, the CORS requests work fine as long as I disable Ripple's cross domain proxy. 有趣的是,如果我从Ripple Emulator运行该应用程序,则只要禁用Ripple的跨域代理,CORS请求就可以正常工作。

Installing the earlier version at work has fixed the problem 在工作中安装早期版本已解决了该问题

$ npm install -g phonegap@3.5.0-0.20.4

The solution was to add the whitelist plugin for the Android build. 解决方案是为Android版本添加白名单插件。 This enabled the urls specified in config.xml 这启用了config.xml中指定的网址

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

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