ajax - cURL POST request in Javascript - Stack Overflow

I am trying to write the following cURL request:$ curl -d @your_filename.json -H "Content-Type: ap

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 and ENTER-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
Add a ment  | 

1 Answer 1

Reset to default 1

Just 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);
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745411674a4626561.html

相关推荐

  • ajax - cURL POST request in Javascript - Stack Overflow

    I am trying to write the following cURL request:$ curl -d @your_filename.json -H "Content-Type: ap

    4小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信