How to get the response text from an AJAX request using jQuery

xennex

I am trying to make a JSONP request to a server. This is my code:

$.ajax({
  type: 'GET',
  url: myURL,
  async: false,
  crossDomain: true,
  dataType: 'jsonp',
  jsonpCallback: 'jsonCallback',
  headers: { 
    'Accept': 'application/json', //this is required by the server
    'key': key   
  },
  success: function() {
    alert('1');
  },
  error: function() {
    alert('2');
  },
  complete: function(){
    alert('3');
  }
});//code indentation

When I run the code it errors. But if I open the developers tools in Chrome (ctrl+shift+I) I can see the request under "network". Clicking on it shows the correct response (and shows the request was accepted).

Apologies is there is a really obvious solution (I have tried searching, but with no luck), but at this point I am well and truly baffled. Any help would be really appreciated.

::EDIT::

changing the error function to:

error: function() {
  console.log('error', arguments);
},

returned the message "jsonCallback was not called" Thanks to Aaron Digulla below.

The response from the server is JSON, not JSONP (checked with JSONlint)

Aaron Digulla

When you say "it errors", my guess is that you get alert(2). To find out why, log the function arguments to the console:

...
error: function() {
    console.log('error', arguments);
},
...

jQuery will pass additional information (like the error message) to the function. That should help you understand why it fails.

The same is true for the success function which gets the server response, for example.

[EDIT]

I get the error jsonCallback was not called

That means your server isn't returning JSONP. JSONP looks like name({...}) while normal JSON looks like {...}. Please check your server's configuration and make sure it actually supports JSONP and that the response looks correct.

I should have seen this from your code:

dataType: 'jsonp'
headers: { 
    'Accept': 'application/json', //this is required by the server
}

That means you're sending a JSONP/JSON mix which can't work. If you use a certain dataType, then let jQuery build the correct headers.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how do i get a response from a jquery ajax request

From Dev

How to get response text from ajax callback in a variable using ExtJS

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to get the ajax response from success and assign it in a variable using jQuery?

From Dev

How to get the data response from ajax request?

From Dev

How to get the data response from ajax request?

From Dev

Using jquery in ajax request to get javascript response UJS

From Dev

How to call ajax request with json response using jquery?

From Dev

How to show json response data in datatable using jQuery ajax request?

From Dev

How to handle 204 response from ajax request in jquery

From Dev

How to get response headers from ajax post request

From Dev

How do I get the response text from a treq request?

From Dev

How to get text response from jquery get method?

From Dev

How to replace text of element after AJAX request using Jquery

From Dev

No response from API using AJAX request

From Dev

How to get data from JSON response using Alamofire request

From Dev

how to get JSON response from servlet to jsp page by using ajax

From Dev

how to get response from node server using ajax method in html

From Dev

How to get response from PHP file as an array using Ajax?

From Dev

How to Make an AJAX HTTPS GET Request Using jQuery

From Dev

jQuery - How to get an element's class from ajax request

From Dev

How can i get text option from AJAX generated select dropdown using jQuery?

From Dev

Why does my jQuery Ajax GET-request not receive a response from the server?

From Dev

AJAX/Jquery - Get response from php file

From Dev

jQuery get response from ajax function

From Dev

How to force string response on a GET request with jQuery?

From Dev

How to get the response title from the "jqXHR" object using jQuery?

From Dev

Get the text inside <h2> element from an ajax jquery post response

From Java

How to get the value of Ajax response in jQuery

Related Related

  1. 1

    how do i get a response from a jquery ajax request

  2. 2

    How to get response text from ajax callback in a variable using ExtJS

  3. 3

    How to get the ajax response from success and assign it in a variable using jQuery?

  4. 4

    How to get the ajax response from success and assign it in a variable using jQuery?

  5. 5

    How to get the data response from ajax request?

  6. 6

    How to get the data response from ajax request?

  7. 7

    Using jquery in ajax request to get javascript response UJS

  8. 8

    How to call ajax request with json response using jquery?

  9. 9

    How to show json response data in datatable using jQuery ajax request?

  10. 10

    How to handle 204 response from ajax request in jquery

  11. 11

    How to get response headers from ajax post request

  12. 12

    How do I get the response text from a treq request?

  13. 13

    How to get text response from jquery get method?

  14. 14

    How to replace text of element after AJAX request using Jquery

  15. 15

    No response from API using AJAX request

  16. 16

    How to get data from JSON response using Alamofire request

  17. 17

    how to get JSON response from servlet to jsp page by using ajax

  18. 18

    how to get response from node server using ajax method in html

  19. 19

    How to get response from PHP file as an array using Ajax?

  20. 20

    How to Make an AJAX HTTPS GET Request Using jQuery

  21. 21

    jQuery - How to get an element's class from ajax request

  22. 22

    How can i get text option from AJAX generated select dropdown using jQuery?

  23. 23

    Why does my jQuery Ajax GET-request not receive a response from the server?

  24. 24

    AJAX/Jquery - Get response from php file

  25. 25

    jQuery get response from ajax function

  26. 26

    How to force string response on a GET request with jQuery?

  27. 27

    How to get the response title from the "jqXHR" object using jQuery?

  28. 28

    Get the text inside <h2> element from an ajax jquery post response

  29. 29

    How to get the value of Ajax response in jQuery

HotTag

Archive