2015-01-15 90 views
1

我一直在試圖獲得一個簡單的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. 

尋找在這個問題上有所幫助。

回答

1

解決了這個問題。 使用RIPPLE仿真器時,SETTINGS-> CROSS DOMAIN PROXY = REMOTE

其他方式的跨域請求被阻止。

4

找到了答案here。更改跨域代理設置爲「遠程」「禁用」。默認情況下,它被設置爲「本地」,它不允許跨域資源訪問。

enter image description here

+0

謝謝你的回答非常有幫助 – IamStalker 2015-03-22 20:10:30