Sam
Sam

Reputation: 113

$.ajax or $.get function get receive only on local files

I need to use $.ajax or $.get to get fetch response from xml file. The thing is that $.ajax or $.get function does not seem to get a response from files that are in remote server. It apparently works on local files only. I am developing the websites from local server, so my serverhost is like 127.0.0.1/. Does anyone know about this problem? Or is there anyway that I can debug this problem at all? I think I have read somewhere where browsers do not allow cross-site-ajax call..but..then how can I make remote host call?

<script>
$(document).ready(function(){
    $.ajax({
        url: "<?=$request_url?>",
        data: "<?=$data?>",
        success: function(msg){
         alert( "Data Saved: " + msg );
        },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
             $("#output").append(XMLHttpRequest.responseText + "<br />TextStatus: " + textStatus + "<br />ErrorThrown: " + errorThrown);
        }
    })
});
</script>

Upvotes: 1

Views: 478

Answers (4)

Paul Rowland
Paul Rowland

Reputation: 8352

Have a look at using JSONP

What is JSONP?
Cross-domain JSONP with jQuery call step-by-step guide

Upvotes: 3

Clyde Lobo
Clyde Lobo

Reputation: 9174

Browsers don't allow cross-domain ajax calls due to security restrictions

But there are still some workarounds in the wild. Try These

http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/

http://usejquery.com/posts/9/the-jquery-cross-domain-ajax-guide

Upvotes: 1

Russell Dias
Russell Dias

Reputation: 73372

Fortunately, you cannot due to the cross-domain policy. You can however invoke a server side script via your $.ajax call which in turn calls on the remote site script.

Upvotes: 0

Paul Mendoza
Paul Mendoza

Reputation: 5787

Yes, this is correct because you can't access content on another domain using those methods. This is a cross domain scripting security issue.

Take a look at XSS in relation to jQuery and Javascript.

Upvotes: 1

Related Questions