0

I am trying to get xml through ajax like below in my javascript code

$(document).ready(function(){

$.ajax({
        url: 'https://santander.easycruit.com/intranet/intranett/export/xml/vacancy/list.xml',
        cache: false,
        dataType: 'xml',
        crossDomain: true,
        success: function (xml) {
            debugger;
            $(xml).find('Vacancy').each(function () {
                    $(this).find("Location").each(function () {
                        var name = $(this).text();
                        alert(name);
                    });
            });



        },
        statusCode: {
            404: function () {
                debugger;
                alert('Failed');
            }
        }
    });
});

but when i run code i get error XMLHttpRequest cannot load https://santander.easycruit.com/intranet/intranett/export/xml/vacancy/list.xml?_=1460979186038. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://mymachinename' is therefore not allowed access

You can see there is some number appended to url like _=1460979186038

Is it because of this i am getting error.

James
  • 1,827
  • 5
  • 39
  • 69
  • This answer -> http://stackoverflow.com/a/36742124/4870935 is correct, however the error you are getting is because of CORS - check this out -> http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work – Mark Apr 20 '16 at 11:17

3 Answers3

1

The _=1460979186038 part is added by jquery ajax, as the mechanism to prevent cache. If I remember currectly that number is just a random + timestamp or something like that.

source: http://api.jquery.com/jquery.ajax/

The reason you are getting the error is No 'Access-Control-Allow-Origin' header is present on the requested resource, which means you are trying to send cross-domain messages but the server didn't allow it.

Ronen Ness
  • 9,923
  • 4
  • 33
  • 50
  • the url in my question actually will work on ur end as well as its public... can you tell how can i access this through ajax? – James Apr 20 '16 at 11:23
  • i have no control over that server – James Apr 20 '16 at 11:23
  • @happy sorry I didn't notice you put "crossDomain: true,", you do request cross domain but the server probably don't allow it in the response. – Ronen Ness Apr 20 '16 at 11:23
  • Maybe make something like proxy service on your localhost (where you store that javascript file), so you can call that website via curl and return the value. – Mark Apr 20 '16 at 11:28
  • @Mark can you give me examples on what are you trying to tell – James Apr 20 '16 at 11:29
  • @Happy I've added answer ilustrating how to do it. – Mark Apr 21 '16 at 05:18
0

It's clearly that you are facing with the issue with cross domain request. If you can control this server, you will need add the header in your server to allow cross domain or for testing purpose, you can use my add on in firefox to development and deal with CORS: https://addons.mozilla.org/en-US/firefox/addon/cross-domain-cors/?src=ss

Tan Mai Van
  • 657
  • 6
  • 8
0

Based on comments discussion I think you should make something like proxy server. Try this PHP code:

<?php
header("Content-type: text/xml");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://santander.easycruit.com/intranet/intranett/export/xml/vacancy/list.xml");
$output = curl_exec($ch);

It would simply fetch desired XML from passed URL and display it on the page. Put the script on the same server your javascript is and call your server with ajax. This should eliminate CORS limitations.

Mark
  • 1,357
  • 16
  • 30
  • For .NET I'd suggest using `System.Net.WebClient()`. The mechanism is the same: download the content and then display it. – Mark Apr 21 '16 at 07:01
  • Can you provide me example or code for it? I am really struggling with this for long time now – James Apr 21 '16 at 07:14