gpt4 book ai didi

javascript - ajax 请求在 OS X Paw 应用程序和 cURL 中工作,但不能像 Javascript/jQuery 那样工作

转载 作者:行者123 更新时间:2023-11-28 00:08:26 24 4
gpt4 key购买 nike

我对 ajax 请求比较陌生,并且正在使用 Paw 应用程序来尝试调试一个请求。该请求在 Paw 本身中工作正常,并且 Paw 生成的 cURL 也工作正常。但是,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 不知道您正在寻找哪个城市。

关于javascript - ajax 请求在 OS X Paw 应用程序和 cURL 中工作,但不能像 Javascript/jQuery 那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31126796/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com