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
398 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 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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

Question has a verified solution.

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

Suggested Solutions

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

734 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