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
389 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 33

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
jQuery Animation faster 5 43
Where do I learn ES6? 3 51
How  can i sort Jquery Datatable currency Column properly? 9 38
AL3 Files 4 14
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

23 Experts available now in Live!

Get 1:1 Help Now