weixin_33720956 2016-10-06 09:39 采纳率: 0%
浏览 36

我的ajax方法不起作用,

In console log getting the bellow message Reason: CORS header ‘Access-Control-Allow-Origin’ missing So I checked few stack overflow solution but not able to resolve some one can help me to resolve it.

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
    $(document).ready(function () 
    {
    var save_temp = {
                 "token": "e2c420d928d4bf8ce0ff2ec19b371514"
                  };
             var comment_data = JSON.stringify(save_temp);

    //alert(comment_data);
    var request = $.ajax({
       url: "http://vyhub.com/irmtapi/dailyreport/checkReport",
       type: "POST",
       data:comment_data,
       contentType: "application/json; charset=utf-8",
       dataType: 'json',
    });

    request.done(function(msg)
    {
      // $("#log").html( msg );
    alert(msg)
    });

    request.fail(function(jqXHR, textStatus) {
       alert( "Request failed: " + textStatus );
    });

    });

    </script> 
  • 写回答

1条回答 默认 最新

  • Lotus@ 2016-10-06 09:54
    关注

    This is clearly a problem with the server side code, which doesn't send the right headers to allow the Cross Origin Request Sharing. You need to enable CORS through two ways in Apache Server with PHP.

    Method 1: Using Apache Server's settings.

    Using Apache's .htaccess, you can enable the CORS by adding the following line:

    Header set Access-Control-Allow-Origin "*"
    

    For this to work, you need to make sure that the mod_headers module is enabled. You can enable this by using sudo a2enmod headers. You need sudo permissions for this. Also, you need to restart Apache Server after changing the configuration by using sudo service apache2 reload.

    Method 2: Using PHP Code.

    Alternatively, if you don't have much control over the server management, you can enable it in your script that responds to your request. Just add this at the top of the PHP file, so that the browser is allowed AJAX Access:

    <?php
      header("Access-Control-Allow-Origin: *");
    

    Let me know if you have any further questions. For more information, please find the references below:

    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀