0

I'm trying to GET xml from a server by using jquery ajax. I can get it working if I host an xml file (containing the same xml data) somewhere myself, but when I try to get it from the (target) server, I receive no data. But if I put the server url in my browser I do get the xml, so the url is fine.

My code:

$.ajax({
    async: false,
    type:"GET",
    url: url,
    crossDomain: true,
    data: "user=user1&password=password1&id=2",
    dataType:"xml",
    success: function(data) { alert(data);},
    error: function(event, jqXHR, ajaxSettings, thrownError) {
        console.log(JSON.stringify(event, null, 4));
        console.log(JSON.stringify(jqXHR, null, 4));
        console.log(JSON.stringify(ajaxSettings, null, 4));
        console.log(JSON.stringify(thrownError, null, 4));
        console.log(JSON.stringify(this, null, 4));
    },

How do I debug this? The GET request returns a statuscode 200:OK. The response headers show a content-length of 22194, but I get no response body? The output I get from the console logging is:

"{
    "readyState": 0,
    "status": 0,
    "statusText": "[Exception... \"Failure\"  nsresult: \"0x80004005 (NS_ERROR_FAILURE)\"  location: \"JS frame :: http://myownurl.com/jquery.js :: .send :: line 8526\"  data: no]"
}"
""error""
"{}"
undefined
"{
    "url": "http://targeturl.com/api/get?user=user1&password=password1&id=2",
    "type": "GET",
    "isLocal": false,
    "global": true,
    "processData": true,
    "async": false,
    "contentType": "application/x-www-form-urlencoded; charset=UTF-8",
    "accepts": {
        "*": "*/*",
        "text": "text/plain",
        "html": "text/html",
        "xml": "application/xml, text/xml",
        "json": "application/json, text/javascript",
        "script": "text/javascript, application/javascript, application/ecmascript, application/x-ecmascript"
    },
    "contents": {
        "xml": {},
        "html": {},
        "json": {},
        "script": {}
    },
    "responseFields": {
        "xml": "responseXML",
        "text": "responseText"
    },
    "converters": {
        "text html": true
    },
    "flatOptions": {
        "url": true,
        "context": true
    },
    "jsonp": "callback",
    "crossDomain": true,
    "dataType": "xml",
    "dataTypes": [
        "xml"
    ],
    "hasContent": false
}"

(I'm not sure if it makes any difference, but I'm trying this in FireFox)

Niels
  • 1,340
  • 2
  • 15
  • 32
  • 1
    open the error console. I expect to see something along the lines of "Access Denied due to Same-Origin Policy", and if you google that, you'll get a full explanation of why this is happening and how to solve it. – Kevin B Feb 19 '14 at 15:15
  • with error console you mean the web console? I have that open, that is where got that console output from. I don't see any same-origin problem. I've tried it with getting an xml file hosted on my google drive, and that worked, so cross domain shouldn't be a problem right? – Niels Feb 19 '14 at 15:19
  • That depends on whether or not the server hosting the xml file is properly implementing cors. the `crossDomain` parameter you are using is irrelevant for true cross-domain requests. – Kevin B Feb 19 '14 at 15:20
  • Also, there's a typo in your code above, most likely a copy-paste error. You're missing `data:` before your paramstring. – Kevin B Feb 19 '14 at 15:22
  • Yes that was a copy-paste error. (don't want to paste passwords here.) I guess you are right. Is there a specific way i can test to confirm that this is the case? – Niels Feb 19 '14 at 15:27
  • Try the same test in chrome or IE and check their consoles. (F12) – Kevin B Feb 19 '14 at 15:28
  • Ah, yes Chrome shows: "XMLHttpRequest cannot load http://targeturl.com/api/get. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://myownurl.com' is therefore not allowed access." – Niels Feb 19 '14 at 15:34
  • If you don't control the server serving the xml, your only option is to request it using the server local to the page that needs it. Otherwise, all you have to do is properly implement cors. – Kevin B Feb 19 '14 at 15:36
  • I don't understand what you mean with 'using the server local'? But I'll contact the guy that controls the server. Should you write all this as an answer so I can accept it? – Niels Feb 19 '14 at 15:50
  • possible duplicate of [jQuery $.ajax(), $.post sending "OPTIONS" as REQUEST\_METHOD in Firefox](http://stackoverflow.com/questions/1099787/jquery-ajax-post-sending-options-as-request-method-in-firefox) – Kevin B Feb 19 '14 at 15:51

0 Answers0