Reputation: 451
I have a problem as follows:
<script>
$(document).ready(function(){
$.ajax({
url: "https://api.domain.com/message.asp",
type: "POST",
cache: false,
contentType: "text/xml",
dataType: "text",
data : "<?xml version=\"1.0\"?>
<mainbody>
<header>
<company>companyName</company>
<usercode>323423</usercode>
<password>543543543</password>
<startdate>010120150100</startdate>
<stopdate>170820150100</stopdate>
<type>1</type>
</header>
</mainbody>
",
crossDomain:true,
success: function(result){
alert(result);
},
error: function(result) {
console.log(result);
}
});
});
</script>
In the code above, the line starting with xml tag returns syntax error as follow: Uncaught SyntaxError: Unexpected token ILLEGAL What is wrong in here?
Upvotes: 0
Views: 125
Reputation: 492
You are using new lines in a variable. Try this code snippet:
<script>
$(document).ready(function(){
$.ajax({
url: "https://api.domain.com/message.asp",
type: "POST",
cache: false,
contentType: "text/xml",
dataType: "text",
data : "<?xml version=\"1.0\"?>\
<mainbody>\
<header>\
<company>companyName</company>\
<usercode>323423</usercode>\
<password>543543543</password>\
<startdate>010120150100</startdate>\
<stopdate>170820150100</stopdate>\
<type>1</type>\
</header>\
</mainbody>\
",
crossDomain:true,
success: function(result){
alert(result);
},
error: function(result) {
console.log(result);
}
});
});
</script>
Though I think the data would be more clear when written like this:
xml = "<?xml version=\"1.0\"?>"
+ "<mainbody>"
+ " <header>"
+ " <company>companyName</company>"
+ " <usercode>323423</usercode>"
+ " <password>543543543</password>"
+ " <startdate>010120150100</startdate>"
+ " <stopdate>170820150100</stopdate>"
+ " <type>1</type>"
+ " </header>"
+ "</mainbody>";
And then use variable xml
as the data
-attribute.
Upvotes: 1
Reputation: 24648
The issue is with the data. That's a very long string literal. When you have a string that spanning several lines, you can either use "first line part" +\n "second line part" +\n..
or First line part\\n Second line part\\n .....
. Try this instead:
data : "<?xml version=\"1.0\"?>\
<mainbody>\
<header>\
<company>companyName</company>\
<usercode>323423</usercode>\
<password>543543543</password>\
<startdate>010120150100</startdate>\
<stopdate>170820150100</stopdate>\
<type>1</type>\
</header>\
</mainbody>\
",
Ref: Creating multiline strings in JavaScript
Upvotes: 1