AJAX 非同步傳輸資料的問題

來源:互聯網
上載者:User

要非同步傳輸的資料:
Xml代碼
....
<action xsi:type="basic:JavaScript" script="index += 1;"/>
....
Ajax非同步傳輸代碼:
Js代碼 複製代碼 代碼如下:var postData = "input="+ escape(inputJSON) +"&script="+escape(xml)+
"&feedGeneral=" + escape(feedGeneral);
XmlHttpRequest.open("POST",url,true);
XmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
XmlHttpRequest.send(postData);

postData在encode和unencode,最終導致在後台Servlet中得到得到資料+被空格代替,使得script中的index += 1;變成了index = 1;從而導致後台Java代碼在跑script出現死迴圈。
在網上搜尋,發現content-type使用application/x-www-form-urlencoded後:
[來自http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1]寫道 複製代碼 代碼如下:Control names and values are escaped. Space characters are replaced by `+', and then reserved characters are escaped as
described in [RFC1738], section 2.2: Non-alphanumeric characters are replaced by `%HH', a percent sign and two hexadecimal
digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., `%0D%0A').

然而使用form來提交方式來發起request卻不會出現類似的問題,而form預設的Content-Type也是application/x-www-form-urlencoded:
Js代碼 複製代碼 代碼如下:$('test').innerHTML = "<form target='_blank' id='test_form' action='./gen_feed' method='post'>"
+ "<input type='text' name='input' /><input type='text' name='script' />"
+ "<input type='text' name='feedGeneral' /><input type='hidden' name='format' value='" + this.feed_type + "'
/>"
+ "<input type='submit' value='gen' /></form>";
var test_form = $('test_form');
test_form.elements[0].value = inputJSON;
test_form.elements[1].value = script;
test_form.elements[2].value = feedGeneral;
test_form.submit();

仍未發現問題到底出在何處,暫做備忘。暫時把script中的‘+'都用‘-'代替,index += 1;改成index -= -1;呵呵,以後有人看到這段自動產生的詭異指令碼,不知道會作何感想,但現在也只能如此。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.