admin管理员组文章数量:1426473
I am trying to write the following cURL
request:
$ curl -d @your_filename.json -H "Content-Type: application/json" -i ""
In Javascript:
$.ajax({
data: JSON.stringify(data),
url: "",
type: 'POST',
dataType: 'json',
contentType: 'application/json',
success: function() {
console.log(data);
},
error: function(){
alert("wrong");
}
});
This all came up together after stumbling through Stack overflow (all qns here were about posting login and pass). This request should send the information in json
format to Google and the response should e, also in json
. But nothing happens.
Should there something to be done to show the response? I thought ajax uses XmlHttpRequest
, so it should work without any explicit task to show the response.
I would be grateful for any help!
I am trying to write the following cURL
request:
$ curl -d @your_filename.json -H "Content-Type: application/json" -i "https://www.googleapis./geolocation/v1/geolocate?key=ENTER-KEY"
In Javascript:
$.ajax({
data: JSON.stringify(data),
url: "https://www.googleapis./geolocation/v1/geolocate?key=ENTER-KEY",
type: 'POST',
dataType: 'json',
contentType: 'application/json',
success: function() {
console.log(data);
},
error: function(){
alert("wrong");
}
});
This all came up together after stumbling through Stack overflow (all qns here were about posting login and pass). This request should send the information in json
format to Google and the response should e, also in json
. But nothing happens.
Should there something to be done to show the response? I thought ajax uses XmlHttpRequest
, so it should work without any explicit task to show the response.
I would be grateful for any help!
Share Improve this question edited May 4, 2018 at 9:30 coder 8,73216 gold badges41 silver badges54 bronze badges asked May 4, 2018 at 9:24 NataschaNatascha 891 gold badge3 silver badges8 bronze badges 2-
Have you replaced the
your_filename
andENTER-KEY
values? Also, the response should be shown in the console of your browser (hit F12 in chrome/firefox) – Max Maximilian Commented May 4, 2018 at 13:48 - Hey! Thank you, of course I did. And I looked up in a console, but there was nothing – Natascha Commented May 5, 2018 at 14:33
1 Answer
Reset to default 1Just in case someone has the same problem as I did. You should use fetch() for such things it's really handy:
var url = 'https://www.googleapis./geolocation/v1/geolocate?key=YOUR_KEY';
var data = {some: json};
fetch(url, {
body: JSON.stringify(data),
headers: {
'dataType': 'json',
'content-type': 'application/json'
},
method: 'POST',
redirect: 'follow'
})
.then(response => {
if (response.status === 200) {
console.log(response.text());
} else {
throw new Error('Something went wrong on api server!');
}
})
.catch(error => {
console.error(error);
});
本文标签: ajaxcURL POST request in JavascriptStack Overflow
版权声明:本文标题:ajax - cURL POST request in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745411673a2657497.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论