ajax post data 獲取不到數據


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

 

 1      var Skip = 49; //Number of skipped row
 2      var Take = 14; //
 3      function Load(Skip, Take) {
 4           $('#divPostsLoader').html('<img src=http://www.update8.com/Web/Jquery/"ProgressBar/ajax-loader.gif">');
 5          //send a query to server side to present new content
 6          $.ajax({
 7               type: "get",
 8               url: "AjaxImage.ashx",
 9              data: { Skip: Skip, Take: Take },
10               //contentType: "application/json; charset=utf-8",//(可以)
11              //contentType: "text/xml",//(可以)
12               //contentType:"application/x-www-form-urlencoded",//(可以)
13              //dataType: "string",
14               success: function (data) {
15                   if (data != "") {
16                      $('.thumb').append(data);
17                   }
18                  $('#divPostsLoader').empty();
19               }
20           })
21      };
View Code

 



chrome下,沒有設置contentType的值,好,我們來看默認情況:

image

默認參數通過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的值,來看默認情況:

image

 

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

好了,默認情況下一般處理文件獲取參數也可以。

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

image

Request Paload 是什么???

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

image

好吧, 到這里我們解決了 博問里那個問題(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)。

            做過模擬登錄、模擬提交數據的同學肯定都很清楚了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM