2016-09-07 98 views
0

我使用的三層體系結構,並試圖使用jquery AJAX將數據存儲到數據庫中,但我沒有得到在成功的函數的響應將數據插入SQL數據庫使用jQuery AJAX在asp.net C#

這裏是我的代碼

portalDAL.cs

public DataTable InsertFeedBack(String Name, String Email, string Category, string Message) 
    { 
    SqlParameter[] parms = new SqlParameter[]{ 


    new SqlParameter("@Name",Name), 
    new SqlParameter("@Email",Email), 
    new SqlParameter("@Category",Category), 
    new SqlParameter("@Message",Message) 

    }; 
    return Helper.ExecuteParamerizedSelectCommand("insert into feedback(name,email,category,message) values(@Name,@Email,@Category,@Message)", CommandType.Text, parms); 
    } 

portalBAL.cs

public DataTable InsertFeedBack(String Name, String Email, string Category, string Message) 
    { 
    return portalDAL.InsertFeedBack(Name, Email, Category, Message); 

    } 

portal.asmx.cs

[WebMethod] 
    public String InsertFeedBack(String Name, String Email, string Category, string Message) 
    { 
    DataTable dt = detailsBAL.InsertFeedBack(Name, Email, Category, Message); 
    return JsonConvert.SerializeObject(dt); 
    } 

我的Jquery函數。

$(document).ready(function() { 
    $('#submit').click(function() { 

    var name = $('#name').val(); 
    var email = $('#email').val(); 
    var category = $('#cate').val(); 
    var msg = $('#msg').val(); 

    insertFeedback(name,email,category,msg); 
    }); 



function insertFeedback(name,email,cat,msg) 
    { 
    $.ajax({ 
    type: "POST", 
    url: "portal.asmx/InsertFeedBack", 
    data: "{'Name':'" + name + "','Email':'" + email + "','Category':'" + cat + "','Message':'" + msg + "'}", 

    contentType: "application/json; charset=utf-8", 
    dataType: "json", 
    success: function (data) { 
    alert("hi"); 
    var obj = data.d; 
    if (obj == 'true') { 
    $('#name').val(''); 
    $('#email').val(''); 
    $('#cate').val(''); 
    $('#msg').val(''); 
    $('#lblmsg1').html("Details Submitted Successfully"); 
    window.location.reload(); 
    } 
    }, 
    error: function (result) { 
    alert("Error"); 
    } 
    }); 

    } 
    }); 

我收到錯誤提示消息控件沒有進入成功的功能它沒有顯示任何錯誤的瀏覽器

+1

你檢查過螢火蟲控制檯什麼錯誤來自服務器?如果是的話,你可以發佈錯誤消息或錯誤文本的屏幕截圖嗎? – iSensical

+0

它沒有在控制檯上顯示任何錯誤..在獲得錯誤消息警報後,將能夠在控制檯上看到此錯誤「未捕獲的異常無法轉換爲字符串」 – user2882173

+0

存在一些服務器端錯誤。把一個調試點放在你的web方法上,並檢查它出現這個錯誤的地方。 – iSensical

回答

0

嘗試使用這個Ajax代碼格式。

$.ajax({ 
    type: "POST", 
    dataType: "json", 
    contentType: "application/json; charset=utf-8", 
    data: "{'Name':'" + name + "','Email':'" + email + "','Category':'" + cat + "','Message':'" + msg + "'}", 
    url: "portal.asmx/InsertFeedBack", 
    success: function (data) { 
     console.log(data); 
    }, 
    error: function (error) { 
     console.log(error); 
    } 
}); 
+0

謝謝將用這種格式檢查 – user2882173

1

嘗試使用此代碼

$('#submit').click(function() { 
    insertFeedback(); 
}); 

function insertFeedback() 
{ 
var model = new Object(); 
    model.name = $('#name').val(); 
    model.email = $('#email').val(); 
    model.category = $('#cate').val(); 
    model.msg = $('#msg').val(); 

$.ajax({ 
type: "POST", 
url: "portal.asmx/InsertFeedBack", 
data: model, 
dataType: "json", 
success: function (data) { 
    alert("hi"); 
    // your code 
    }, 
error: function (result) { 
    alert("Error"); 
} 
} 

創建一個類有四個屬性

public class YourClass 
{ 
public string name { get; set; } 
public string email { get; set; } 
public string category { get; set; } 
public string message { get; set; } 
} 

改變你的方法參數類對象。您可以從一個對象接收來自ajax調用的參數數量。

[WebMethod] 
public String InsertFeedBack(YourClass model) 
{ 
DataTable dt = detailsBAL.InsertFeedBack(model.name, model.email, model.category, model.message); 
return JsonConvert.SerializeObject(dt); 
} 
+1

謝謝你會通過它 – user2882173

相關問題