2017-06-04 75 views
0

這是我的代碼 我使用Jquery Ajax將數據從一個頁面提供給另一個 。在提供數據時不會發生。Web Api控制器在發佈請求期間未觸發

$("#button").click(function() { 
      var test = $("#name").val(); 
      var test1 = $("#username").val(); 
      var test2 = $("#email").val(); 
      var test3 = $("#pwd").val(); 
      var data1 = { 
       name: test, 
       username: test1, 
       email: test2, 
       password:test3 
      } 
      alert("the value is" + test + "" + test1 + "" + test2 + "" + test3); 
      $.ajax({ 
       type: 'POST', 
       url: 'http://localhost:54150/api/Login', 
       data:data1, 
       contentType: 'application/json;charset=utf-8', 
       success: function (result) { 
        alert('Data Saved Successfully'); 
       }, 
       error: function() { 
        alert("Bad...Really bad"); 
       } 
      }); 
     }); 

和我的控制器

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Web.Http; 
using System.Web.Mvc; 
using System.Web.UI.WebControls; 
using AbiApp.Models; 

namespace AbiApp.Controllers 
{ 
    public class LoginController : ApiController 
    { 
     String connStr = System.Configuration.ConfigurationManager.ConnectionStrings["CS"].ConnectionString; 
     [System.Web.Http.AcceptVerbs("GET", "POST")] 
     [System.Web.Http.HttpPost] 

     public void Post([FromBody]login loginmodel) 
     { 
      try 
      { 
       using (SqlConnection df = new SqlConnection(connStr)) 
       { 
        using (SqlCommand de = new SqlCommand()) 
        { 
         de.CommandType = System.Data.CommandType.StoredProcedure; 
         de.CommandText = "StoreImageDetails"; 
         de.Connection = df; 
         de.Parameters.Add(new SqlParameter("@name", loginmodel.name)); 
         de.Parameters.Add(new SqlParameter("@username", loginmodel.username)); 
         de.Parameters.Add(new SqlParameter("@email", loginmodel.email)); 
         de.Parameters.Add(new SqlParameter("@password", loginmodel.password)); 
         df.Open(); 
         var dt = de.ExecuteNonQuery(); 
        } 
       } 
      } 
      catch (Exception ed) 
      { 
       //ed+Message;  
      } 
     } 
    } 
} 

從jQuery阿賈克斯 叫做請檢查代碼並 請找錯誤

+0

試試,loginmodel應該出來,如果它擊中空。同時發佈Login類結構 – Gururaj

回答

1

似乎問題與您的路由時,不會打。您可以刪除

[System.Web.Http.AcceptVerbs("GET", "POST")]

和使用屬性通過Ajax調用刪除數據路由如下

[Route("api/Login")] 
public void Post([FromBody]login loginmodel) 
     { 
      try 
      { 
       using (SqlConnection df = new SqlConnection(connStr)) 
       { 
        using (SqlCommand de = new SqlCommand()) 
        { 
         de.CommandType = System.Data.CommandType.StoredProcedure; 
         de.CommandText = "StoreImageDetails"; 
         de.Connection = df; 
         de.Parameters.Add(new SqlParameter("@name", loginmodel.name)); 
         de.Parameters.Add(new SqlParameter("@username", loginmodel.username)); 
         de.Parameters.Add(new SqlParameter("@email", loginmodel.email)); 
         de.Parameters.Add(new SqlParameter("@password", loginmodel.password)); 
         df.Open(); 
         var dt = de.ExecuteNonQuery(); 
        } 
       } 
      } 
      catch (Exception ed) 
      { 
       //ed+Message;  
      } 
     }