【问题标题】:Jquery AJAX XML data returns syntax errorJquery AJAX XML 数据返回语法错误
【发布时间】:2015-08-17 22:04:59
【问题描述】:

我有一个问题如下:

<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 标签开头的行返回语法错误如下: Uncaught SyntaxError: Unexpected token ILLEGAL 这里有什么问题?

【问题讨论】:

    标签: jquery ajax xml xmlhttprequest


    【解决方案1】:

    您正在变量中使用新行。试试这个代码 sn-p:

    <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-属性。

    【讨论】:

      【解决方案2】:

      问题在于数据。这是一个很长的字符串文字。当您有一个跨越多行的字符串时,您可以使用"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

      【讨论】:

        猜你喜欢
        • 2018-04-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-03
        • 2012-01-08
        • 2014-01-27
        相关资源
        最近更新 更多