2015-11-26 29 views
1

我想發送數據到服務器腳本mail.php與我的離子應用程序。 但在功能handleErrornull的「數據」的說法......角度http發佈,錯誤數據爲空

你能幫我解決這個問題?

它可能是內容安全協議問題嗎? 因爲我不確定在這裏使用什麼...... 我需要與boardlineapp.com進行交流,並使用FB和Google Single Sign In。

controllers.js:

sendToServer.f(data).success(handleSuccess).error(handleError); 
    function handleSuccess(data , textStatus, jqXHR) { 
     console.log("Message successfully sent"); 
     $("#mypanel").panel("close"); 
    } 
    function handleError(data , textStatus, jqXHR ) { 
     console.log("Error when sending the message : "); 
     console.log(data); 
     console.log(data.response); 
     console.log(textStatus); 
     console.log(jqXHR); 
     alert("The message could not be sent. Sorry for the inconvenience."); 
    }; 

services.js:

.service('sendToServer', function sendToServerFactory($http) { 
    this.f = function(dataToSend) { 
     return $http({ 
      url: 'http://id:[email protected]/app/mail.php', 
      method: "POST", 
      data: dataToSend 
     }); 
    } 
}) 

mail.php:

<?php 
header("Access-Control-Allow-Origin: *"); 
header("Access-Control-Allow-Methods: GET, POST"); 
header("Access-Control-Allow-Headers: *"); 
if($_POST) { 
    $userEmail=$_POST['userEmail']; 
    $subject=$_POST['subject']; 
    $destEmail=$_POST['destEmail']; 
    $body=$_POST['body']; 

    mail($destEmail, $subject, $body, "From:" . $userEmail); 
#this bit doesn't work 
# $response = mail($destEmail, $subject, $body, "From:" . $userEmail); 
# echo $response; 
# exit(); 
} 
?> 

控制檯錯誤:

error TypeError: Cannot read property 'response' of null 
    at handleError (http://192.168.0.11:8100/js/services.js:201:27) 


XMLHttpRequest cannot load http://boardlineapp.com/app/mail.php. 
Response to preflight request doesn't pass access control check: The 
'Access-Control-Allow-Origin' header contains multiple values '*, *', 
but only one is allowed. Origin 'http://192.168.0.11:8100' is 
therefore not allowed access. 

config.xml文件:

<access origin="*"/> 
<allow-navigation href="*"/> 
<allow-intent href="*"/> 
+0

它實際上看起來像是一個跨站點源安全(CORS)問題,請嘗試配置您的服務器以獲取適當的CORS標頭,因爲這應該可以解決這個問題,或者至少可以解決下一個問題。 –

+0

@PaulRyan我刪除了這一行'header(「Access-Control-Allow-Origin:*」);'在mail.php中,它似乎現在可以工作。你可以請我向我解釋如何'配置我的服務器正確的CORS',因爲我不知道如何做到這一點。對不起,我是新手。 – Louis

+0

很高興工作,這裏要記住的是,您允許或禁止來自多個來源(boardlineapp.com和192.168.0.11:8100)的腳本訪問服務器上的內容。這是默認不允許的。通過啓用CORS *表示您不關心安全性的這一方面。這對於開發人員來說可以,但可能不適合生產。您必須查看服務器的具體信息以瞭解如何正確配置(例如apache,nginx,IIS等)。在http://stackoverflow.com/q/19322973上提供有關含義的體面回答。 –

回答

1

的類型錯誤是紅鯡魚在這個真正的錯誤是造成你的問題是訪問控制允許來源問題。這是指向'*,*'服務器的錯誤配置,您不能複製相同的原點,因此您必須更新您的配置(這取決於服務器的類型,例如nginx,apache,IIS等)。

接下來了解設置原產地策略的設置是個好主意。這是說你允許訪問你的服務器腳本,在這種情況下,你試圖加載兩個不同的來源boardlineapp.com和192.168.0.11:8100,這就是爲什麼我會假設你首先啓用*的原因。這對於您的開發服務器來說是一個很好的解決方案,安全性可能不是一個大問題,但您希望在生產中更具體。

stackoverflow.com/q/19322973上的問題和答案詳細介紹了此問題的安全隱患。