Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
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
Medium Priority
?
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 1000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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.
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

721 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