關於Ajax的type為post提交方式出現請求失效問題

來源:互聯網
上載者:User

標籤:erro   限制   如何   flow   escape   .com   cti   提交資料   集合   

最近碰到這樣一個問題,原本一個ajax非同步提交資料之前是很好的,很多系統都延用了均未出現任何問題。最近這個版本卻出現ajax方式post資料不到背景程式,讓我折騰了好幾天。起初的ajax是這樣寫的:

 

view sourceprint?01.$.ajax({02.type: ‘POST‘,03.url: "/Ajax/SaveData.aspx",04.dataType: "json",05.data: "RequestTime="+ (new Date()).toString()+"&WorkFlowInfo="+jsonText,06.beforeSend: function () {07.},08.success: function (msg) {09.$("div[class=‘datagrid-mask‘]").hide();10.$("div[class=‘datagrid-mask-msg‘]").hide();11.alert(msg.mes);12.if (msg.op != "err")13.window.location.reload();14. 15.},16.error: function (errorMsg) {17.$("div[class=‘datagrid-mask‘]").hide();18.$("div[class=‘datagrid-mask-msg‘]").hide();19.alert("發生了一個錯誤");20.}21.});

 

當我的jsonText字串不是很長的情況下,是可以post至後台服務的。那麼既然我的type為post方式,與資料長短有何關係對吧?你或許會如此堅定的想。

易錯點:

那麼為何當jsonText過長時post提交至後台失敗呢?細心的網友可能會發現雖然是post方式但是data的書寫方式是不對的,data原本就是接受json類型的參數和資料的,上面的代碼卻將之當做了直接在url內傳遞參數的形式,換言之就是將ajax的psot提交方式活生生地變換為了get方式提交。

get方式提交是直接在url內傳遞參數和資料,url內有最大字數限制的,所以當jsonText過長時就提交不至後台服務。

 

如何糾正?

既然是data的書寫方式不對,那麼我們只需要糾正一下這個地方即可,糾正後的範例程式碼如下所示:

 

view sourceprint?01.$.ajax({02.type: ‘POST‘,03.url: "/Ajax/SaveData.aspx",04.dataType: "json",05.data: {RequestTime:(new Date()).toString(),WorkFlowInfo:escape(jsonText)},06.beforeSend: function () {07.},08.success: function (msg) {09.$("div[class=‘datagrid-mask‘]").hide();10.$("div[class=‘datagrid-mask-msg‘]").hide();11.alert(msg.mes);12.if (msg.op != "err")13.window.location.reload();14. 15.},16.error: function (errorMsg) {17.$("div[class=‘datagrid-mask‘]").hide();18.$("div[class=‘datagrid-mask-msg‘]").hide();19.alert("發生了一個錯誤");20.}21.});

這樣一來就可以正常post資料至後台服務了的。

關於Ajax的type為post提交方式出現請求失效問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.