0

In JavaScript I get JSON data. The service sends me back JSON data in a file (HTTP Header - file), a second service sends me back data as text in browser (HTTP Header type - text/html).

I need to parse this data in JavaScript (ExtJS).

It is different to Javascript, getting JSON as text/html and as file?

I know that with text/html - all works fine, but will it work with other HTTP Header type (that retrieves file that contains JSON)?

Thanks

isNaN1247
  • 17,793
  • 12
  • 71
  • 118
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286

2 Answers2

1

It should be application/json.

If it is anything else, it might break clients that try to detect the result type automatically. But if a client expects JSON, they will probably ignore the header anyway.

Community
  • 1
  • 1
Thilo
  • 257,207
  • 101
  • 511
  • 656
1

If your response content-type is text/html you should be fine, as long as you parse the response with something like JSON.parse (see json2.js for older browsers that don't have this natively) it should be fine.

In fact, if you're loading your response from an iframe - IE will prompt you to download a file if your response is application/json. This should only be an issue if you're attempting a file upload without refreshing the page though.

To be honest I've never tried with other header types. I would imagine text/plain could work, however you may run into the IE iframe download issue above.

My recommendation would be to use application/json if you can, however failing this text/html should suffice

Community
  • 1
  • 1
isNaN1247
  • 17,793
  • 12
  • 71
  • 118