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:

    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上