Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use json and when jsonp with jquery $.ajax?

In our site, Some pages are SSL and some are non-SSL.

For Example:

http://www.example.com/search/patients
https://www.example.com/patients

Now I am searching patients on http://www.example.com/search/patients page and send server request to https://www.example.com/patients via jQuery $.ajax function and dataType=json. I am unable to get data.

Questions:

  1. Should I use jsonp when we request from http to https or https to http on same server?

  2. If I use SSL for both URLs then will it work with dataType=json only

Thanks

like image 658
Awan Avatar asked Jan 19 '26 21:01

Awan


2 Answers

Due to Same Origin Policy your ajax request is allowed only if: domain name, application layer protocol, and (in most browsers) port number of the HTML document running the script are the same

In your case the application layer protocol is different, that's why your script fails.

Possible solutions are:

  • JSONP, which has to be provided by the server

  • CORS, which is a more 'elegant' and clean solution, but is not yet fully supported by IE (IE7 doesn't support it, IE8 has some limitations)

like image 71
mamoo Avatar answered Jan 21 '26 11:01

mamoo


If you use SSL for both URLs it should work. Also as @Waqas Raja suggested, it would be better to use relative URLS.

e.g. $.ajax({url: '/search/patients'})

like image 30
paulslater19 Avatar answered Jan 21 '26 10:01

paulslater19