0

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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.