I use following code for get json encoded result
$.get('localhost/hospital/index/json', function(data) {
alert(data);
});
It is work for localhost URL. But I want get result from external server. I try following example url to do it.
$.get('hospitalsystem.dev/index/json', function(data) {
alert(data);
});
But it is not working. Please help me.
Omitting the
http://from the URL, as in your code snippet, can prevent the request from working, as shown in this example.AJAX requests are limited by Same Origin policy, meaning that you can only make requests to the same domain. There are three main ways to get around this:
JSONP involves adding an argument to an API to function as a javascript file. JQuery can make this as simple as a normal AJAX request. A big problem with this is that it requires you to trust the provider of the API you’re calling not to do anything malicious, such as transmitting cookies back to them.
Cross-Origin Resource Sharing is a more elegant solution, but only works in some browsers.
Creating a proxy for AJAX requests works with virtually any API, but adds some additional overhead and having two requests instead of one for each AJAX request may impact performance.