Solved

sending XML string to an API returns an XML error of XML not well formed. Missing root element

Posted on 2011-03-16
3
385 Views
Last Modified: 2013-11-05
I'm sending a well formed validate XML string to an CRM's API.  I use jQuery 1.4.3 and a $.ajax() call.  However the API failed to load the XML string as it says the XML is not well formed.  the API support team tell me the XML string has no root element based on their error logs.  

My XML string has a root element so some confusion here...

I use Safari and after checking the header returned when the API fails I noticed the root element is incorrect even though I'm sending it correct.

For example:

var xmlString = '<?xml version="1.0" ?><request><password>xx</password><action>add</action><contact><prefix></prefix><firstname>'+firstname+'</firstname><lastname>'+lastname+'</lastname><address1></address1><address2></address2><city></city><county></county><postcode></postcode><country>United Kingdom</country><email>'+email+'</email><workphone></workphone><homephone>'+tel+'</homephone><mobilephone>'+mobile+'</mobilephone><dg_media>0</dg_media><adm_email_status>normal</adm_email_status><adm_post_status>normal</adm_post_status><adm_sms_status>normal</adm_sms_status><cr1></cr1><cr2></cr2><cr3></cr3><cr4></cr4><cr6></cr6><cr7></cr7><cr8></cr8><cr9></cr9><cr10></cr10><cr11></cr11><cr12></cr12><cr13></cr13><cr14></cr14><interaction><interactiontitle>General Contact Form</interactiontitle><interactiondesc>'+message+'</interactiondesc></interaction></contact></request>';
                          
                          //console.log(xmlString);
                   
                              $.ajax(
                            {
                                cache:false,
                processData:false,
                type:"POST",
                url:"http://domain.com/api/add.asp",
                data:xmlString,
                success:function(data,textStatus,XMLHttpRequest){},
                    error:function(XMLHttpRequest,textStatus,errorThrown){alert(XMLHttpRequest + "\n\n" + textStatus + "\n\n" + errorThrown);},
                    complete:function(XMLHttpRequest,textStatus){}
});


The incorrect form data POSTed...  Not sure what this is happening - notice the <?xml version:"1.0" ?> instead of <?xml version="1.0" ?>

<?xml version:"1.0" ?><request><password>xx</password><action>add</action><contact><prefix></prefix><firstname>test</firstname><lastname>user</lastname><address1></address1><address2></address2><city></city><county></county><postcode></postcode><country>United Kingdom</country><email>email@domain.com</email><workphone></workphone><homephone>0</homephone><mobilephone>0</mobilephone><dg_media>0</dg_media><adm_email_status>normal</adm_email_status><adm_post_status>normal</adm_post_status><adm_sms_status>normal</adm_sms_status><cr1></cr1><cr2></cr2><cr3></cr3><cr4></cr4><cr6></cr6><cr7></cr7><cr8></cr8><cr9></cr9><cr10></cr10><cr11></cr11><cr12></cr12><cr13></cr13><cr14></cr14><interaction><interactiontitle>General Contact Form</interactiontitle><interactiondesc>Test%20Message%205</interactiondesc></interaction></contact></request>;

I've never seen this before and a little confused.  I believe this is whats causing my CRM API request to fail.

Please advise?  
0
Comment
Question by:ellandrd
3 Comments
 
LVL 32

Expert Comment

by:shalomc
ID: 35155595
Is the semicolon a part of the posted data?
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 35155880
Try :


var xmlString = '<request><password>xx</password><action>add</action><contact><prefix></prefix><firstname>'+firstname+'</firstname><lastname>'+lastname+'</lastname><address1></address1><address2></address2><city></city><county></county><postcode></postcode><country>United Kingdom</country><email>'+email+'</email><workphone></workphone><homephone>'+tel+'</homephone><mobilephone>'+mobile+'</mobilephone><dg_media>0</dg_media><adm_email_status>normal</adm_email_status><adm_post_status>normal</adm_post_status><adm_sms_status>normal</adm_sms_status><cr1></cr1><cr2></cr2><cr3></cr3><cr4></cr4><cr6></cr6><cr7></cr7><cr8></cr8><cr9></cr9><cr10></cr10><cr11></cr11><cr12></cr12><cr13></cr13><cr14></cr14><interaction><interactiontitle>General Contact Form</interactiontitle><interactiondesc>'+message+'</interactiondesc></interaction></contact></request>';

$.ajax( 
                            {
dataType:"xml"
                                cache:false,
                processData:false,
                type:"POST",
                url:"http://domain.com/api/add.asp",
                data:xmlString,
                success:function(data,textStatus,XMLHttpRequest){},
                    error:function(XMLHttpRequest,textStatus,errorThrown){alert(XMLHttpRequest + "\n\n" + textStatus + "\n\n" + errorThrown);},
                    complete:function(XMLHttpRequest,textStatus){}
});

Open in new window

0
 
LVL 16

Author Closing Comment

by:ellandrd
ID: 35283202
Solution no longer required but thanks for time and advise
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
How to build a simple, quick and effective accordion menu using just 15 lines of jQuery and 2 css classes
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now