ajax post data 擷取不到資料,注意content-type的設定post/get

來源:互聯網
上載者:User

  ajax post  data  擷取不到資料,注意 content-type的設定 、post/get
關於 jQuery data 傳遞資料。網上各種擷取不到資料,亂碼之類的。
好吧今天我也遇到了,網上一查各種糾結。亂碼不管先看擷取不到資料。
因為之前一直用jQuery ajax get的方式傳遞參數, 預設沒有設定過 contentType 的值。

      var Skip = 49; //Number of skipped row
     var Take = 14; //
     function Load(Skip, Take) {
          $('#divPostsLoader').html('<img src=http://www.update8.com/Web/Jquery/"ProgressBar/ajax-loader.gif">');
         //send a query to server side to present new content
         $.ajax({
              type: "get",
              url: "AjaxImage.ashx",
             data: { Skip: Skip, Take: Take },
              //contentType: "application/json; charset=utf-8",//(可以)
             //contentType: "text/xml",//(可以)
              //contentType:"application/x-www-form-urlencoded",//(可以)
             //dataType: "string",
              success: function (data) {
                  if (data != "") {
                     $('.thumb').append(data);
                  }
                 $('#divPostsLoader').empty();
              }
          })
     };

chrome下,沒有設定contentType的值,好,我們來看預設情況:



預設參數通過url參數傳遞,請求的內容類型:application/x-www-form-urlencoded

一般處理檔案擷取參數內容:

int Skip = Convert.ToInt32(context.Request["Skip"]); 2: int Take = Convert.ToInt32(context.Request["Take"]);

毫無壓力,因為我一直都是這麼乾的大笑,沒有任何問題。好了,來換一下請求的內容類型:
 //contentType: "application/json; charset=utf-8",//(可以) 2: //contentType: "text/xml",//(可以)
 

也都可以,參數擷取正常。
這也就是我們說的get方式,參數是跟在url後邊,與Content-Type無關。

 

可是今天要用post方式了有木有。

$.ajax({ 2: type: "post",
chrome下,沒有設定contentType的值,來看預設情況:


 

data資料由from表單提交,請求的內容類型:application/x-www-form-urlencoded,

好了,預設情況下一般處理檔案擷取參數也可以。

可是,但是 我最開始設定的是 contentType: "application/json; charset=utf-8",看圖:



Request Paload 是什嗎???

調試一下,看我們的from裡邊,沒有內容:



好吧, 到這裡我們解決了 博問裡那個問題(http://q.cnblogs.com/q/11342/),

本來搜尋到這個博問,甚是高興的,但是,懸而未決有木有,小夥伴不厚道啊!捧腹大笑

經測試:

              //contentType: "application/json; charset=utf-8",//(不可以)
              //contentType: "text/xml",//(不可以)
             contentType:"application/x-www-form-urlencoded",//(可以)

 

總結一下吧:本來get/post方式都是知道的,但注意,contentType與傳遞資料匹配(本文data)。

            做過類比登入、類比提交資料的同學肯定都很清楚了。



相關文章

聯繫我們

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