简体   繁体   English

角度http发布,错误数据为null

[英]angular http post, error data is null

I am trying send post data to a server script mail.php with my ionic app. 我正在尝试使用mail.php应用程序将发布数据发送到服务器脚本mail.php But the 'data' argument in the function handleError is null ... 但是函数handleError的'data'参数为null ...

Can you help me fix this? 你能帮我解决这个问题吗?

Can it be a Content Security Protocol issue? 可以是内容安全协议问题吗? Because I am not sure what to use here... I need to communicate with boardlineapp.com and use FB and Google Single Sign In. 因为我不确定在这里使用什么...我需要与boardlineapp.com交流并使用FB和Google Single Sign In。

controllers.js: 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: services.js:

.service('sendToServer', function sendToServerFactory($http) {  
    this.f = function(dataToSend) {
        return $http({
            url: 'http://id:password@boardlineapp.com/app/mail.php',
            method: "POST",
            data: dataToSend
        });
    }
})

mail.php: 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();
}
?>

Console error: 控制台错误:

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: config.xml文件:

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

The TypeError is a red-herring in this the real error that's causing an issue for you is the Access-Control-Allow-Origin issue. TypeError是一个红色提示,导致您出现问题的真正错误是Access-Control-Allow-Origin问题。 This is pointing to a mis-configuration for server of '*, *', you can't duplicate the same origin so you'll have to update your configuration (this is dependent on the type of server, eg nginx, apache, IIS, etc). 这表明服务器的配置错误,“ *,*”,您不能复制相同的来源,因此必须更新配置(这取决于服务器的类型,例如nginx,apache,IIS等)。

Next it's a good idea to understand what setting the origin policy is doing. 接下来,最好了解设置源策略的作用。 This is saying where you're allowing access to your server script through in this case your trying to load two different origins boardlineapp.com and 192.168.0.11:8100 which is why I'd assume you enabled * in the first place. 这就是说,在这种情况下,您尝试通过加载两个不同的来源boardlineapp.com和192.168.0.11:8100来访问服务器脚本,这就是为什么我假设您首先启用*的原因。 This is a great work around for your development server where security may not be a big concern but you'll want to be much more specific in production. 对于您的开发服务器来说,这是一个很好的解决方法,在该服务器中,安全性不是一个大问题,但您希望在生产中更加具体。

The question and answers at stackoverflow.com/q/19322973 go into good detail about the security implications of this problem. stackoverflow.com/q/19322973上的问题和答案详细介绍了此问题的安全性。

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

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