【问题标题】:Ajax Post request always run error function although curl to the same url works fineAjax Post 请求总是运行错误函数,尽管 curl 到相同的 url 工作正常
【发布时间】:2016-05-27 04:26:57
【问题描述】:

我正在使用 ajax 的 post 方法从服务器获取 json 结果。但是当我单击 HTML 页面上的按钮时,ajax 函数总是运行错误函数,也就是说,它总是显示警告说“错误”。我在此处粘贴我的 HTML 正文供您查看。

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Form</title>
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head> 
<body>
    <div id="main">
        <div id="header">

        </div>
        <div id="form">
            <table style="display:inline-block">
                <tr>
                    <td>Select a user:</td>
                    <td>
                        <select id="SelectUser" style="width:150px">
                            <option>User 1</option>
                            <option>User 2</option>
                            <option>User 3</option>
                            <option>User 4</option>
                            <option>User 5</option>
                            <option>User 6</option>
                            <option>User 7</option>
                            <option>User 8</option>
                            <option>User 9</option>
                            <option>User 10</option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Action:</td>
                    <td>
                        <select id="SelectAction" style="width:150px">
                            <option>Buy</option>
                            <option>Sell
                            <option>
                        </select>
                    </td>
                </tr>
                <tr>
                    <td>Quantity:</td>
                    <td><input id="txtQty" type="text" /></td>
                </tr>
                <tr>
                    <td colspan="2" style="text-align:right">
                        <input id="btnClear" type="button" value="Clear" style="width:100px" onclick="alert('Clear')" />
                        <input id="btnTrade" type="button" value="Trade" style="width:100px" onclick="getTrade()" />
                    </td>
                </tr>
            </table>
            <div id="data">
                <b>Recommendations</b>
                <hr />
            </div>
        </div>
    </div>

    <script>
        function getTrade() {               
            $.ajax({                
                type: "POST",
                url:  "http://localhost:8000/queries.json/",    
                data: {user: "1", num: 4},
                success: function (resultData) {                    
                    alert("resultData");                 


                },
                error: function (xhr,thrownError){
                    alert("error");
                }
            }); 

        }
    </script>   
</body>
</html>

当我运行 curl 请求时 (curl -H "Content-Type: application/json" \ -d '{ "user": "1", "num": 4 }' http://localhost:8000/queries.json) 在我的 git shell 中,它工作正常并按预期返回结果。

有什么想法吗?

【问题讨论】:

  • queries.json是json文件还是控制器方法?
  • 您可能遇到了同源策略。
  • @ user2181397 - 我不确定 query.json 文件。我怎么知道它是否是控制器?
  • @SLaks - 你能解释一下那是什么,我怎样才能以正确的方式做到这一点?谢谢

标签: javascript jquery json ajax curl


【解决方案1】:

当我使用 Firefox firebug 调试问题时,我开始了解错误的详细信息:

跨域请求被阻止:同源策略不允许读取位于http://localhost:8000/queries.json/ 的远程资源。 (原因:CORS 标头“Access-Control-Allow-Origin”缺失)。

看起来我需要在客户端和服务器上启用 CORS。这样就可以解决问题了。

感谢 SLaks 为我指明了正确的方向。

【讨论】:

    猜你喜欢
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-01
    • 2018-07-28
    • 1970-01-01
    • 2017-09-01
    • 2013-02-11
    • 1970-01-01
    相关资源
    最近更新 更多