0

I am creating a project in MVC that involves using AJAX to grab XML from an external url. The issue is that I can't do something like this:

$.ajax({
  url: url,
  data: data,
  success: success,
  dataType: dataType
});

because the site I'm accessing for the xml is public and I get a XMLHttpRequest same domain policy error. My solution is to run a server side script (like PHP?) to grab the XML info and then forward it to be used by my program (a Google Maps project with marker data that updates every 15 seconds).

My question is, how would I incorporate the script to get the XML to my project? Can I use PHP? ASPX? I've never done anything quiet like this, so I just don't have a clue on how it would all fit together.

For clarification this is what I was doing:

public class HomeModel
{
    public string XmlData { get; set; }

    public void GetXml()
    {
        XmlDocument doc1 = new XmlDocument();
        doc1.Load("http://www.nfl.com/liveupdate/scorestrip/ss.xml");
        XmlData = doc1.InnerXml;
    }
}

But this won't work because I want to use AJAX to grab the XML data so that I don't have to do a full page refresh.

2
  • If you're already using ASP.Net in your project, why not just use that to create the proxy? Commented Feb 20, 2015 at 16:34
  • I updated the OP to include what I am currently doing, but it isn't what I want because I want to update the data "in the background" (via AJAX) instead of a full page refresh. Commented Feb 20, 2015 at 16:44

2 Answers 2

2

Well I guess the issue here is more of an ethical one. The owner of the site you are trying access the data from doesn't want you to directly access that data and re-render to another client. Hence the same domain policy error.

You could still download the file on your server side. I.E. in C# you could user HttpClient.

HttpClient client = new HttpClient();
string file = await client.GetStringAsync('http://www.example.com/myfile.xml');

However if you actually controller the server where the xml file is being served from you should just enable cors (Cross-origin resource sharing) of the xml file on your server.

EDIT

Based on your edit you want to create an MVC Action that calls your code. You could then make your ajax call on your own server side code, thus negating the cors issue.

The url www.mysite.com/MyXML would now return the file from http://www.nfl.com/liveupdate/scorestrip/ss.xml.

public class MyXMLController : Controller
{
    public ActionResult Index()
    {
        XmlDocument doc1 = new XmlDocument();
        doc1.Load("http://www.nfl.com/liveupdate/scorestrip/ss.xml");
        XmlData = doc1.InnerXml;
        return Content(doc1.InnerXml, "text/xml", System.Text.Encoding.UTF8);
    }
}

NOTE: Ethically I do not agree with this approach.

Sign up to request clarification or add additional context in comments.

2 Comments

That will allow me to update marker info every X seconds without having to refresh the entire page? I guess I'm confused on how AJAX would fit in with this.
You would just do the AJAX request that you would have made to http://www.nfl.com/liveupdate/scorestrip/ss.xml but instead you would make the request to www.mysite.com/MyXML. Your server side code then requests the file and returns it to your client.
1

Assuming I'm getting this right. You want to do a 15 mins client side poll of a service that will get you an XML? and the client you are requesting data from is complaining about cross site scripting?

You could potentially do an ajax call to one of your own endpoint on the same application and in there you do a http call:

var myHttpClient = new HttpClient();
var response = await myHttpClient.PostAsync(uri, Content)

You do need to build your content. There are multiple ways of doing it depending on what it is (form, string, binary, json).

Front End ===(ajax)===> MVC endpoint ===(httpclient/webclient)===> Third party

Front End <===(response)=== MVC endpoint <===(response)=== Third party

2 Comments

I want to grab XML from a site every 15 seconds and so that I can use it for my Google Maps project. The entire point is to update map marker data without having to do a full page refresh, therefore AJAX seems to be the way to go. A coworker explained that the only way to achieve this is to run a server side script to get the XML, then "forward" it to my application. I'm not sure if your solution would allow for that?
Yes, your colleague is right. But if you can't do an ajax call to a different site because the other site is refusing to, then you can do an ajax call to an endpoint on your site (same domain). What that enpoint (on your site) does is pretty much a call to the url of the third party and return is as a response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.