2015-10-15 43 views
1

我有一個web服務,並在其中有兩種方法(select,insertdata)。我想用jquery在sql中插入一條記錄。我怎樣才能做到這一點?我已經制作了該代碼,但它不起作用。請幫助我。我如何添加項目在sql中使用jQuery(ajax)通過web服務

我的web服務

Sqlconeection dd = new Sqlconeection(); 
int rowsInserted = 0; 
[WebMethod] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public string insertRecord(Int64 mbn,string name,double amt,bool Notify) 
{ 
    SqlConnection connection = dd.getconnection(); 
    SqlCommand cmd = new SqlCommand("InsertData"); 
    cmd.Connection = connection; 
    if (connection.State == ConnectionState.Closed) 
     connection.Open(); 
    // string inser = "insert into expensive(mobileNumber,Name,Amount,Notify)values('" + mbn + "','" + name + "','" + amt + "','" + Notify + "')"; 
    //cmd.CommandText = inser; 
    cmd.Parameters.AddWithValue("@mobile", mbn); 
    cmd.Parameters.AddWithValue("@Name", name); 
    cmd.Parameters.AddWithValue("@Amount", amt); 
    cmd.Parameters.AddWithValue("@Notify", Notify); 
    cmd.CommandType = CommandType.StoredProcedure; 
    rowsInserted= cmd.ExecuteNonQuery(); 
    return string.Format("Thank you, {0} number of rows inserted!", rowsInserted); 
} 
[WebMethod(Description = "Returns all Products")] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
public DataTable selectdata(Int64 mnb) 
{ 
    DataTable dt = new DataTable(); 
    SqlConnection conn = dd.getconnection(); 
    SqlCommand cmd = new SqlCommand(); 
    cmd.Connection = conn; 
    if (conn.State == ConnectionState.Closed) 
     conn.Open(); 
    string select = "select mobileNumber,Name,Amount,Notify from expensive where mobileNumber='" + mnb + "'"; 
    cmd.CommandText = select; 
    SqlDataReader dr = cmd.ExecuteReader(); 
    dt.TableName = "expensive"; 
    dt.Load(dr); 
    return dt; 

} 

我API代碼

$(function() { 
    $('#btnSubmit').click(function() { 
     var mob = $('#txtmo').val(); 
     var Nm = $('#txtName').val(); 
     var amout = $('#txtAmt').val(); 
     var notify = $('#txtnoty').val(); 
     $.ajax({ 
      type: "POST", 
      url: "InsertData.asmx/insertRecord", 

      data: "{ mob: '" + mob + "', Nm: '" + Nm + "',amount:'" + amout + "',notify:'" + notify + "'}", 
      contentType: "application/json; charset=utf-8", 
      dataType: "jsonp", 
      success: function (data) { 
       var obj = data.d; 
       if (obj == 'true') { 
        $('#txtmo').val(''); 
        $('#txtName').val(''); 
        $('#txtAmt').val(''); 
        $('#txtnoty').val(''); 
        $('#lblData').html(JSON.stringify(data.d)); 
       } 
      }, 
      error: function (r) { 
       console.log(r); 
      }, 
     }); 
    }); 
}); 
+0

請提供一些更多的細節,並顯示爲代碼剪斷,而不是整個程序。 – Sojtin

+0

您的數據對象屬性名稱與您webmethod的啓動項不匹配。 – kmcoulson

+0

同樣在您的成功中,您檢查webmethod是否返回'true',但它實際上會返回成功消息。 – kmcoulson

回答

0

你需要一個ScriptService屬性添加到Web服務類:

[ScriptService] 
public class InsertData : WebService 

然後更換你的javascr ipt與此:

$(function() { 
    $('#btnSubmit').click(function() { 
     var mob = $('#txtmo').val(); 
     var name = $('#txtName').val(); 
     var amout = $('#txtAmt').val(); 
     var notify = $('#txtnoty').is(':checked'); 

     var submission = { 
      'mbn': mob, 
      'name': name, 
      'amt': amout, 
      'notify': notify, 
     }; 

     $.ajax({ 
      type: "POST", 
      url: "InsertData.asmx/insertRecord", 

      data: JSON.stringify(submission), 
      contentType: "application/json; charset=utf-8", 
      success: function (data) { 
       if (data != null) { 
        console.log(data); 
        $('#txtmo').val(''); 
        $('#txtName').val(''); 
        $('#txtAmt').val(''); 
        $('#txtnoty').val(''); 
        $('#lblData').html(data); 
       } 
      }, 
      error: function (r) { 
       console.log(r); 
      }, 
     }); 
    }); 
}); 
+0

它不工作。給我另一個解決方案。告訴我一個想法。它有必要添加Web服務引用或不在客戶端.. –

+0

是調用與服務本身在同一目錄中的服務頁面? – kmcoulson

+0

我已經更新了上面的解決方案,我已經測試過了,它絕對有效。 – kmcoulson

相關問題