C# webApi----H5_app後台製作(2)--參數的傳輸(對於懶得安分webApi參數傳輸不行的補充)

來源:互聯網
上載者:User

標籤:images   --   oca   dea   2.0   blog   路徑   app   control   

(2)問題二:傳參的方式;

http://www.cnblogs.com/landeanfen/p/5337072.html對於這篇文章的補充

 測試一如下:

1)後端代碼如下,

注意1:Api中的方法中的參數只能有一個,不能出現比如像這樣

public object post(LoginInfo user,string name)
 
 public class LoginController :ApiController    {        [HttpPost]        public object post(LoginInfo user)        {            return user.strPwd;        }       public class LoginInfo    {        public string strUser { get; set; }        public string strPwd { get; set; }    }}

發送請求如下:以json格式發送資料,自己寫的要求標頭,注意contextType:application/json,代表以json的格式發送資料。

注意1:預設的路徑設定為 routeTemplate: "api/{controller}/{id}",

要設定為 routeTemplate: "api/{controller}/{action}/{id}",才能請求的方法

接收到的返回的資料成功。

使用jquery測試,發起post請求:直接發送對象格式的資料,jquery會自動根據發送的格式轉化成相應的格式發送出去

 代碼如下:

    <script src="jquery-2.2.0.min.js"></script>    <script>        var postdata = { strUser: "13443", strPwd: "Jim" };        function Login() {            $.ajax({                type: "post",                url: "http://localhost:61083/api/Login/Login",               // contentType: "application/json",                data: postdata,                success: function (data, status) {                               }            });        }    </script></head><body>    <div style="text-align:center;">        <div>使用者名稱:<input type="text" id="txt_username" value="admin" /></div>        <div>密  碼:<input type="password" id="txt_password" value="123456" /></div>        <div><input type="button" value="登入" id="btn_login" class="btn-default" onclick="javaScript: Login()" /></div>    </div></body>

 

 

C# webApi----H5_app後台製作(2)--參數的傳輸(對於懶得安分webApi參數傳輸不行的補充)

相關文章

聯繫我們

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