doujuxin7392 2016-05-17 10:16
浏览 27
已采纳

JavaScript MISBEHAVING - > CROSS ORIGIN RESOURCE EXCEPTION

After long research and debugging i realized java-script was not the problem, I was rather having a cross origin resource exception which was thrown because some how the ajax request could not read my server script or rather did not have permission to access my server script. So i tried the following codes below but still the same issue, I placed the issue in red block blow, my java script is also below and my server script

$('#send').on('click', function(e) {
  var name = $('#name').val();
  var email = $('#email').val();
  var message = $('#message').val();
  alert("sending " + name + email + message);// this part is now working fine
  $.ajax({
    type: "POST",
    url: "https://xxxxxx/xxxxx/php/contact/maincontact.php",
    data: {
      nme: name,
      ema: email,
      msg: message
    },
    dataType: 'json',
    timeout: 10000,
    async: true,
    cache: true,// I removed the header which used to here before
    error: function(jqXHR, textStatus, errorThrown) {
      alert("error : " + errorThrown + " text :" + textStatus + " j :" + jqXHR.status);
      alert(jqXHR.responseText);
    },
    success: Succeeded
  });

});


function Succeeded(result) {
  alert("Successing");
  var data = JSON.parse(result[0]);
  try {
    $('#name').val(" ");
    $('#email').val(" ");
    $('#message').val(" ");
    if (data == true) {
      alert("We will Contact you shortly");
    } else {
      alert("OOpps! something went wrong");
    }

  } catch (e) {
    alert("You may not enter " + e);
  }
}

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://xxxxxxxx/xxxxxx/php/contact/maincontact.php. (Reason: missing token 'access-control-allow-origin' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel).

<!-- php codes -->
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
require_once('contact.php');

if(!empty($_POST)){

    $addcase = new contact();
    $data['vlu'] = $addcase->add($_POST['nme'], $_POST['ema'],     $_POST['msg']);
    echo json_encode($data);

}else{
   echo "Not properly parsed";
}


?>
  • 写回答

1条回答 默认 最新

  • douzhong1907 2016-05-17 12:10
    关注

    The error message says:

    missing token 'access-control-allow-origin' in CORS header 'Access-Control-Allow-Headers' from CORS preflight channel

    So when you make the request you are trying to put access-control-allow-origin in the headers. (The message means that browser is asking the server if it is allowed to send that header in cross-origin Ajax and the server isn't saying that it is OK to do that).

    This doesn't make any sense. Access-Control-Allow-Origin is a response header. There is no reason to put it on the request.

    Now, the code you've provided doesn't try to set that header. This suggests that elsewhere in your code you are trying to set it universally (probably via ajaxsetup).

    You need to find the point where you try to set it and remove that code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog