WCF Service returns an XML, how to prase using jquery?
i am calling an api for registration and if everything goes well then api returns an xml file
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\u000a<successNotification>\u000a\u0009<message>Successful web registration<\/message>\u0开发者_Python百科00a\u0009<registrationId>22342347<\/registrationId>\u000a<\/successNotification>"
if something wrong then it returns a line of message
you are missing something
my question is: how can i display message regardless whether it returns xml or just line of message:
$.getJSON('http://localhost:4126/service.svc/alert', { phoneNumber: '01231231233' },
function (data) {
var result = $(data).find('message').html;
display.innerHTML = result;
});
Use .get, return a string even when you are returning XML and parse it browser side with .parseXML would be one way.
$.get('http://localhost:4126/service.svc/alert',
{ phoneNumber: '01231231233' },
function (data) {
var result;
if(data.indexOf("<") >= 0) { // test for a character in XML but not the string
var xmlDoc = $.parseXML(data);
result = $(xmlDoc).find('message').html();
} else {
result = data;
}
display.innerHTML = result;
},
"text");
精彩评论