Pri*_*ata 2 php jquery cordova visual-studio-2012
我一直在尝试使用简单的 HTTP POST 来将基于cordova 的应用程序连接到服务器(基于php)。
我编写了一个简单的 php 文件仅用于测试目的。
代码:
<?php
if (isset($_POST["TEST"])){
echo "TEST WORKS!!";
}
?>
Run Code Online (Sandbox Code Playgroud)
现在我的cordova应用程序中的jquery代码>
// For an introduction to the Blank template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkID=397704
// To debug code on page load in Ripple or on Android devices/emulators: launch your app, set breakpoints,
// and then run "window.location.reload()" in the JavaScript Console.
(function () {
"use strict";
document.addEventListener('deviceready', onDeviceReady.bind(this), false);
function onDeviceReady() {
// Handle the Cordova pause and resume events
document.addEventListener('pause', onPause.bind(this), false);
document.addEventListener('resume', onResume.bind(this), false);
// TODO: Cordova has been loaded. Perform any initialization that requires Cordova here.
//My Code
$('#btnSend').on('click', function () {
$.post('http://mobtest.bugs3.com/test.php',
{ TEST: 'TEST' },
function (result) {
alert(result);
$('#txtlbl').text(result);
},
function (error) {
alert(error);
$('#txtlbl').text(error);
}
);
});
};
function onPause() {
// TODO: This application has been suspended. Save application state here.
};
function onResume() {
// TODO: This application has been reactivated. Restore application state here.
};
})();
Run Code Online (Sandbox Code Playgroud)
这是基于 Visual Studio 为 apache/cordova 构建提供的模板构建的。
当我单击该按钮时,我在控制台中看到以下消息:
http://localhost:4400/ripple/xhr_proxy?tinyhippos_apikey=ABC&tinyhippos_rurl=http%3A//mobtest.bugs3.com/test.php Failed to load resource: net::ERR_CONNECTION_REFUSED
Run Code Online (Sandbox Code Playgroud)
哪里 http://mobtest.bugs3.com/test.php
是php文件的url。
编辑
经过一番研究,我发现是跨域代理导致了问题。
现在最新的错误信息是:
XMLHttpRequest cannot load http://mobtest.bugs3.com/test.php. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4414' is therefore not allowed access.
Run Code Online (Sandbox Code Playgroud)
寻求有关此问题的帮助。