我一直在試圖獲得一個簡單的HTTP POST來連接基於cordova的應用程序到服務器(基於php)。在科爾多瓦使用Visual Studio的HTTP POST失敗
我編寫了一個簡單的PHP文件只是爲了測試目的。
PHP代碼:
<?php
if (isset($_POST["TEST"])){
echo "TEST WORKS!!";
}
?>
現在在我的科爾多瓦應用>
// 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.
};
})();
這是建立在由Visual Studio爲Apache /科爾多瓦構建提供的模板jQuery代碼。
當我按一下按鈕,我看到我的控制檯以下消息:
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
凡http://mobtest.bugs3.com/test.php
是PHP文件的URL。
編輯
了一些研究之後,我想通了,它的跨域PROXY,這是造成問題的原因。
現在最新的錯誤信息是:
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.
尋找在這個問題上有所幫助。
謝謝你的回答非常有幫助 – IamStalker 2015-03-22 20:10:30