$ .post在Chrome中被取消

Abh*_*pta 4 javascript php ajax jquery post

我正在调用一个php文件$.post.从服务器我返回json响应.当我直接在浏览器中打开服务器URL时,它会成功返回.但是从js来看它不起作用. 在此输入图像描述 我调用的链接是this(http://54.249.240.120/qcorner/login).

<html>                                                                  
    <head>                                                                  
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    </head>                                                                 
    <body>
        <input type="button" id="testID">
        <script type="text/javascript">                                         
            $(document).ready(function() {
                $("#testID").click(function() {
                    $.post('http://54.249.240.120/qcorner/login',function(result){
                        alert(result);
                    });
                });
            });
         </script>                                                               
     </body>                                                                 
 </html>
Run Code Online (Sandbox Code Playgroud)

我也试过Firefox.在那我得到200 OK,但没有回应.

为什么会这样

dan*_*ith 5

问题是,它是一个跨域请求,可以通过打开javascript控制台查看返回的错误:

XMLHttpRequest cannot load http://54.249.240.120/qcorner/login. Origin null is not allowed by Access-Control-Allow-Origin.

您需要确保将Access-Control-Allow-Origin标头设置为允许这种情况发生,否则Chrome会取消请求.