简体   繁体   中英

Running script with ASP.NET MVC Project

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.

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. IE 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM