I do not believe this will work, as the service still returns xml. jsonp expects an n-object callback to be passed as an argument. I believe that if you run this locally, you will realize that there is no data in your success. try it
$.ajax({ type: "GET", url: "http://services.faa.gov/airport/status/IAD?format=json", dataType: "jsonp", success: function (data) { document.myform.result1.value = data.city; }, error: function (jqXHR, textStatus, errorThrown) { alert(errorThrown); } });
Here is an example of creating a proxy with asp.net mvc 3. I just created an action that returns a ContentResult that maps to a string, but I define the content type as text / xml. It just makes a web request for the service and reads the stream into a string to send back in response.
[HttpGet] public ContentResult XmlExample() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://services.faa.gov/airport/status/IAD?format=xml"); string xml = null; using (WebResponse response = request.GetResponse()) { using (var xmlStream = new StreamReader(response.GetResponseStream())) { xml = xmlStream.ReadToEnd(); } } return Content(xml, "text/xml"); }
Your xmlParser function will look like this:
<script type="text/javascript"> var result; function xmlparser() { $.ajax({ type: "GET", url: "XmlExample", dataType: "xml", success: function (xml) { result = $(xml).find("City").text(); document.myform.result1.value = result; }, error: function (xml) { alert(xml.status + ' ' + xml.statusText); } }); } </script>
jQuery ajax converts the data using $ .parseXML inside, which eliminates the requirement for us to even call this in a success block. At this point, you have a jQuery object that you can use by default for DOM functions to find the City Node.
Be sure to replace XmlExample with the URL that it maps based on your controller.