亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

jQuery向webApi提交post json數(shù)據(jù)

 更新時(shí)間:2017年01月16日 11:41:12   投稿:mrr  
這篇文章主要介紹了jQuery向webApi提交post json數(shù)據(jù)的方法,非常不錯(cuò),需要的的朋友參考下

在頁面想webApi post json數(shù)據(jù)的時(shí)候,發(fā)現(xiàn)webapi不能直接以json的方式接受數(shù)據(jù)(注:我是沒有發(fā)現(xiàn)一個(gè)很好的方式來post json數(shù)據(jù)的);但是可以以數(shù)據(jù)結(jié)構(gòu)的方式傳遞;

如下:

//js代碼 
          var d = {
          Id: "1",
          Name: "name",
          Value: "OldValue", 7         };
        $.ajax({
          type: "post",
          url: url1,
          data: JSON.stringify({
            pConfig: d
          }),
          success:function(d){
          }
        });
public class Diff
  {
    public string Id { set; get; }
    public string Name { set; get; }
    public string Value { set; get; }
  }
 public Diff post([FromBody]Diff pConfig)
    {
      List<DiffConfig> s = pConfig;
      return s;
    }

像這樣的代碼是沒有問題的;得到的是一個(gè)標(biāo)準(zhǔn)結(jié)構(gòu)的數(shù)據(jù);

但是如果改為下面的代碼,就會(huì)發(fā)現(xiàn)沒有數(shù)據(jù)

//js代碼 
         var d = [{
           Id: "1",
           Name: "name",
           Value: "Value",
         },{
           Id: "2",
           Name: "name2",
           Value: "Value2",
         }];
         $.ajax({
          type: "post",
          url: url1,
           data: JSON.stringify({
             pConfig: d
          }),
           success:function(d){
         }
        });
public List<Diff> post([FromBody]List<Diff> diff)
     {
       List<Diff> d = diff;
       return d;
     }

這樣的代碼會(huì)發(fā)現(xiàn),數(shù)據(jù)沒有傳過來,后面才發(fā)現(xiàn),原來jq的ajax傳輸數(shù)據(jù)類型有問題;傳輸?shù)臄?shù)據(jù)類型contentType的默認(rèn)值為 "application/x-www-form-urlencoded"。默認(rèn)值適合大多數(shù)情況。但是卻不能適應(yīng)這次傳輸?shù)闹担?nbsp;  contentType: 'application/json' 設(shè)置一下,就可以ok了;數(shù)據(jù)傳輸完全沒有問題;

$.ajax({
      type: "post",
      dataType: 'json',
      url: url,
      contentType: 'application/json',
      data: JSON.stringify(d),
      success: function (d) {
       
      }
    });

以上所述是小編給大家介紹的jQuery向webApi提交post json數(shù)據(jù),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論