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

本文标签: ajaxcURL POST request in JavascriptStack Overflow