【问题标题】:Send ajax request is not working发送ajax请求不起作用
【发布时间】:2013-08-09 04:37:09
【问题描述】:

我发送以下 curl 请求以从 mongodb 获取结果。

curl --data 'cmd={"geoNear" : "items", "near":[6.8590845,79.9800719]}' 'http://82.196.xxx.xxx:27080/weather/_cmd'

它正在工作。我尝试发送 ajax 请求来完成 curl 请求。

         $.ajax({
            url: 'http://82.196.xxx.xxx:27080/weather/_cmd',
            type: 'POST',
            // dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)',
            data: {"geoNear" : "items", "near":[6.8590845,79.9800719]},
        })
        .done(function() {
            console.log("success");
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });

我认为问题是我的数据对象。 curl 请求有'cmd={"geoNear" : "items", "near":[6.8590845,79.9800719]}。但我不知道它如何转换为 ajax 请求。

但它不起作用。我得到了200 OK 状态。请帮我。

【问题讨论】:

  • 这不是我的问题。我有200状态。但我没有收到成功消息。
  • 试试data: {cmd: JSON.stringify({"geoNear" : "items", "near":[6.8590845,79.9800719]})}
  • 看起来服务器期待一个名为 cmd 的参数,它是一个 json 字符串作为值
  • 还将 fail 日志更改为 console.log("error", arguments);

标签: ajax mongodb jquery curl


【解决方案1】:
 $.ajax({
    url: '/weather/_cmd',
    type:'POST',
    dataType: 'json',
    crossDomain : true,
    data: {
        cmd: JSON.stringify({
            "geoNear" : "items",
            "near": [6.8590845,79.9800719]
        })
    },
    success: function(res) {
        //do stuff with res
    }
});

ProxyPass /weather http://82.196.11.207:27080/weather/_cmd
ProxyPassReverse /weather http://82.196.11.207:27080/weather/_cmd

将上面的代理传递添加到您的 Apache 并尝试这个。它会起作用的。问题是您无法使用 jsonp 传递 POST 请求。 mongo 我们需要 POST 请求。这是一种方法。 :D 我对其进行了测试,并且对我来说非常适合。

-update - 它不会接受 json 并且你必须将它作为字符串传递。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    相关资源
    最近更新 更多