Jquery AJAX XML 数据 returns 语法错误

Jquery AJAX XML data returns syntax error

我遇到的问题如下:

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

上述代码中,以xml标签开头的行returns语法错误如下: 未捕获的语法错误:意外的令牌非法 这里有什么问题?

问题出在数据上。这是一个很长的字符串文字。当您有一个跨越多行的字符串时,您可以使用 "first line part" +\n "second line part" +\n..First line part\n Second line part\n .....。试试这个:

        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>\
                ",

参考:Creating multiline strings in JavaScript

您正在变量中使用换行符。试试这个代码片段:

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

虽然我觉得这样写数据会更清楚:

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>";

然后使用变量 xml 作为 data 属性。