XML Parsing Error: no element found
I have an ASP.Net 4.0 Web Service method that returns a well-formed XML document. I am successfully displaying the XML in a browser locally and once deployed on the production server.
When I try to call the method via jQuery ajax I'm getting the error:
XML Parsing Error: no element found Location: moz-nullprincipal:{6c0c99b3-0fed-454f-aa6e-e0fca93a521c} Line Number 1, Column 1:
$.ajax(
{
url: 'http://mywebservice.com/WebService/Service.asmx/UserData',
type: 'GET',
contentType: "text/html; charset=utf-8",
dataType: "xml",
data: 'authorizedId=1234&authorizedUser=Test&authorizedCode=xyz',
'success': function (data) {
$('#XMLContent').html(data.responseText);
},
'error': function (xhr, status) {
alert(status);
},
'complete': function (xhr) {
开发者_StackOverflow社区 }
});
I've tried changing the contentType but same results.
However, I can make the call in C# like this and I get my well-formed XML:
XmlDocument document = new XmlDocument();
document.Load("http://mywebservice.com/WebService/Service.asmx/UserData?authorizedId=1234&authorizedUser=Test&authorizedCode=xyz");
ViewData["XMLData"] = document.OuterXml;
In my web service web.config:
<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>
Thanks...
If the web service is not on the same domain as the page, you can not use AJAX calls to fetch data from other domains.
You can create a proxy web service in your aplication which calls your external web service, then call your own proxy from AJAX/jQuery.
http://forum.jquery.com/topic/jquery-ajax-and-xml-issues-no-element-found
Hope that helps
Thanks bgs264...
Now in my aspx page:
$.ajax(
{
url: '/Home/WebService',
type: 'GET',
contentType: "text/html",
dataType: "html",
data: 'authorizedId=1234&authorizedUser=Test&authorizedCode=xyz',
'success': function (data) {
alert(data);
$('#XMLContent').html(data);
},
'error': function (xhr, status) {
alert(status);
},
'complete': function (xhr) {
}
});
In my MVC controller:
public ActionResult WebService(string authorizedId, string authorizedUser, string authorizedCode)
{
XmlDocument document = new XmlDocument();
document.Load("http://mywebservice.com/WebService/Service.asmx/UserData?authorizedId=" + authorizedId + "&authorizedUser=" + authorizedUser + "&authorizedCode=" + authorizedCode);
ViewData["XMLData"] = document.OuterXml;
return PartialView();
}
精彩评论