0

I know I must be missing something obvious, but I can't seem to see why my code/approach doesn't work. When I try to call an anonymous function upon success in my AJAX call (I'm just trying to grab an XML file), it never fires. If, however, I change the success to a mere alert('success!') This works fine. Can anyone shed light on why this is happening?

$("#target").click(function() {
    alert( "Handler for .click() called." );
    var part = document.getElementById('partname').value;
    var url = "http://parts.igem.org/xml/part." + part;
    alert(url);

    $.ajax({
        type: 'GET',
        url: url,
        dataType: 'xml',
        success: function(response) {
            console.log('response = '+response);
        }
    });

});

Again, using: success: alert('success');

In lieu of the anonymous function works fine. Completely puzzled over here. Thank you!

Diana E
  • 430
  • 6
  • 21
  • is the URL correct? do just... `console.log(response);` – gloomy.penguin Nov 09 '13 at 03:56
  • The URL looks okay in the sense that when I pasted the url from var url into a browser, I arrive at the XML page. See, example, http://parts.igem.org/xml/part.BBa_B0034 – Diana E Nov 09 '13 at 03:57
  • Okay. Can you do just `console.log(response);` without adding that string to it first? – gloomy.penguin Nov 09 '13 at 04:00
  • Just tried it. Nothing prints to the console. :-/ – Diana E Nov 09 '13 at 04:00
  • this is your site, right? `http://parts.igem.org/` the same domain you're running the ajax from...? – gloomy.penguin Nov 09 '13 at 04:07
  • It is not my site. It is an external site. Is that the problem? – Diana E Nov 09 '13 at 04:08
  • yeah... absolutely. you can't do that. same-origin policy. no cross site scripting. – gloomy.penguin Nov 09 '13 at 04:09
  • http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy – gloomy.penguin Nov 09 '13 at 04:10
  • also... I took a look at the questions you've posted here just to see if I could suggest a good alternative. You should try to accept more answers (green checkmark) from posted answers. If nothing suggested helped, don't feel the need to mark an answer. You can also try using the comments to request more information from the people who posted answers to clarify issues so that your problem was resolved. – gloomy.penguin Nov 09 '13 at 04:16

2 Answers2

2

AJAX calls have to come from the same domain or site because of the same origin policy...

The same origin policy prevents a document or script loaded from one origin from getting or setting properties of a document from another origin. This policy dates all the way back to Netscape Navigator 2.0.

one of the usual ways to get XML from another site is to use PHP and CURL. you have only listed client side languages, though. I would look into other methods of getting this data...

Why does this policy exist?

Community
  • 1
  • 1
gloomy.penguin
  • 5,833
  • 6
  • 33
  • 59
  • Thanks! I'm still pretty new to programming and in the past, incidentally, had executed such calls from the server - without knowing why this worked and did not violate the same origin policy. Thanks for taking the time to point me toward these resources. – Diana E Nov 09 '13 at 04:25
0

You could try implementing CORS, but that would require you to have access to the other domain. You would need to set a response header at the requested domain of "ACCESS-CONTROL-ALLOW-ORIGIN:*" or "ACCESS-CONTROL-ALLOW-ORIGIN:http://www.domainThatMakesTheRequest.com"

cfg
  • 954
  • 7
  • 6