我对ajax请求比较陌生,并且正在使用Paw应用尝试调试一个。该请求在Paw本身中运行良好,并且cURL Paw生成的内容也可以运行。但是,Paw的JavaScript(jQuery)代码不起作用。它获得成功代码(200),但是返回的数据是

{ message: "Error: Not found city", cod: "404" }.


应该返回芝加哥的当前天气。

这是由Paw生成的请求(除了删除多余的换行符和我的Mashape键):

$.ajax({
    url: "https://community-open-weather-map.p.mashape.com/weather",
    type: "POST",
    data: {
        "lang": "en",
        "lat": "41.8369",
        "lon": "-87.6847",
        "units": "metric",
    },
    headers: {
        "X-Mashape-Authorization": "",
    },
    contentType: "application/json",
    data: JSON.stringify({

    })
})
.done(function(data, textStatus, jqXHR) {
    console.log("HTTP Request Succeeded: " + jqXHR.status);
    console.log(data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
    console.log("HTTP Request Failed");
})
.always(function() {
    /* ... */
});


关于可能出问题的任何想法?谢谢!

最佳答案

问题似乎是您覆盖了查询api所用的数据(即您要查询天气的位置)。

$.ajax({
    url: "https://community-open-weather-map.p.mashape.com/weather",
    type: "POST",
    // you set data here:
    data: {
        "lang": "en",
        "lat": "41.8369",
        "lon": "-87.6847",
        "units": "metric",
    },
    headers: {
        "X-Mashape-Authorization": "",
    },
    contentType: "application/json",
    // you overwrite data here with an empty object - so remove this!
    data: JSON.stringify({ // <-- remove
        // remove
    }) // <-- remove
})


只需删除第二个数据属性,它应该可以工作:

$.ajax({
    url: "https://community-open-weather-map.p.mashape.com/weather",
    type: "POST",
    data: {
        "lang": "en",
        "lat": "41.8369",
        "lon": "-87.6847",
        "units": "metric",
    },
    headers: {
        "X-Mashape-Authorization": "",
    },
    contentType: "application/json"
})
.done(function(data, textStatus, jqXHR) {
    console.log("HTTP Request Succeeded: " + jqXHR.status);
    console.log(data);
})
.fail(function(jqXHR, textStatus, errorThrown) {
    console.log("HTTP Request Failed");
})
.always(function() {
    /* ... */
});


当您用空对象覆盖数据时,Open Weather API不会知道您要查找的城市。

09-19 18:04