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

bodacydo

I've written the following code:

$.get('http://example.com/books.json', function (data) {
  // ...
});

books.json is a list of objects of new books, sometimes jQuery parses the JSON data into object, but sometimes leaves as string (probably because of some international/utf8 issues). In that case I call JSON.parse() myself and get valid JSON object that works.

I've a question therefore - what is the correct way to force JSON.parse when doing a GET request with jQuery? I don't want jQuery to guess if it's JSON data or not. How could I make jQuery think it's string always, and I will always call JSON.parse myself?

Barmar

$.get takes an optional dataType argument, and you can specify text to force it to return the data as a plain string.

$.get('http://example.com/books.json', function (data) {
  // ...
}, "text");

Otherwise, it looks at the Content-type: header in the response to determine the datatype.

If you want to force a JSON parse, you can specify json, or you can use $.getJSON.

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 to get the response text from an AJAX request using jQuery

From Dev

how do i get a response from a jquery ajax request

From Dev

how to get response of http request

From Dev

Postman extension get a response, but my jquery request not

From Dev

How to get response with .load jQuery

From Dev

How to get the data response from ajax request?

From Dev

How to get response headers and body in dispatch request?

From Dev

How to get SoapUI request and response XML in java

From Dev

How to get response of vcalendar meeting invitation request

From Dev

How to get response object from POST request?

From Dev

How to get the data response from ajax request?

From Dev

How to get the ExpressJS request and response object in LoopbackJS?

From Dev

How to get full HTTP request (not response) headers

From Dev

How to get a response number of a HTTPS request

From Dev

Using jquery in ajax request to get javascript response UJS

From Dev

Return response of GET request from c++ to QML string

From Dev

Return response of GET request from c++ to QML string

From Dev

How to force a password request when using apt-get install?

From Dev

Handling a get request with a response

From Dev

How to get Response Header information from Spring RestTemplate GET request

From Java

How to get the value of Ajax response in jQuery

From Dev

How to get a raw Retrofit response string?

From Dev

AFNetworking: how to get the response string when failed?

From Dev

AFNetworking: how to get the response string when failed?

From Dev

How to listen for Request complete event? Or how to get response object back

From Dev

How do I get Jersey 2.5.1 on tomcat to log request/response including entities (request/response body)

From Dev

How to handle 204 response from ajax request in jquery

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?

Related Related

  1. 1

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

  2. 2

    how do i get a response from a jquery ajax request

  3. 3

    how to get response of http request

  4. 4

    Postman extension get a response, but my jquery request not

  5. 5

    How to get response with .load jQuery

  6. 6

    How to get the data response from ajax request?

  7. 7

    How to get response headers and body in dispatch request?

  8. 8

    How to get SoapUI request and response XML in java

  9. 9

    How to get response of vcalendar meeting invitation request

  10. 10

    How to get response object from POST request?

  11. 11

    How to get the data response from ajax request?

  12. 12

    How to get the ExpressJS request and response object in LoopbackJS?

  13. 13

    How to get full HTTP request (not response) headers

  14. 14

    How to get a response number of a HTTPS request

  15. 15

    Using jquery in ajax request to get javascript response UJS

  16. 16

    Return response of GET request from c++ to QML string

  17. 17

    Return response of GET request from c++ to QML string

  18. 18

    How to force a password request when using apt-get install?

  19. 19

    Handling a get request with a response

  20. 20

    How to get Response Header information from Spring RestTemplate GET request

  21. 21

    How to get the value of Ajax response in jQuery

  22. 22

    How to get a raw Retrofit response string?

  23. 23

    AFNetworking: how to get the response string when failed?

  24. 24

    AFNetworking: how to get the response string when failed?

  25. 25

    How to listen for Request complete event? Or how to get response object back

  26. 26

    How do I get Jersey 2.5.1 on tomcat to log request/response including entities (request/response body)

  27. 27

    How to handle 204 response from ajax request in jquery

  28. 28

    How to call ajax request with json response using jquery?

  29. 29

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

HotTag

Archive