我在我的頁面上有2個Ajax函數。點擊一個網格內的鏈接,點擊一個按鈕的鏈接。來自電網的那個完美地工作。但是,點擊按鈕的功能每次都會產生錯誤。AJAX函數webmethod不工作
有人可以告訴我在哪裏我錯了。我將不勝感激任何幫助。這是一個失敗的:
function createSource() {
$.ajax({
type: "POST",
url: "Test.aspx/createSource",
data: '{"schoolID":"' + $('#ddSchools').val() + '","vendor":"' + $('#txtVendor').val() + '","tSource":"' + $('#txtTSource').val()+ '"}',
contentType: "application/json",
dataType: "json",
success: function (msg) {
alert(msg.d);
},
error: function (xhRequest, ErrorText, thrownError) {
alert(thrownError);
}
});
}
Webmethod將有更多的代碼,但我似乎無法擊中它。這裏是webmethod:
[WebMethod]
[System.Web.Script.Services.ScriptMethod(UseHttpGet = true)]
public static string createSource(int schoolId, string vendor, string tSource)
{
try
{
string status = "This is a test string!";
return status;
}
catch
{
throw new Exception("Could not create source code!");
}
}
另外我怎麼能得到導致此功能失敗的確切錯誤?
預先感謝您的任何幫助!
好吧,所以我覺得問題在哪裏,但我仍然沒有解決方案。實際上代碼沒有任何問題。我將代碼插入到document.ready中並正確觸發。但是,無論何時點擊按鈕,它都不會正確啓動。按鈕也沒什麼特別。這是一個簡單的輸入按鈕。
<input id="cmdSubmit_Create" value="Submit" type="submit" />
有什麼想法嗎?
僅供參考,我要把這對工作的document.ready代碼:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "Test.aspx/createSource",
data: '{"schoolId":"2236","vendor":"test","tsource":"test1234"}',
contentType: "application/json",
dataType: "json",
success: function (msg) {
alert(msg.d);
},
error: function (jqXHR, textStatus, errorThrown) {
alert("status: " + textStatus + " errorThrown: " + errorThrown);
},
complete: function (jqXHR, textStatus) {
alert("status: " + textStatus);
}
});
});
[WebMethod]
public static string helloWorld(string schoolId, string vendor,string tsource)
{
try
{
StringBuilder sb = new StringBuilder();
sb.Append("Hello World! " + schoolId + '-' + vendor + '-' + tsource);
return sb.ToString();
}
catch
{
throw new Exception("Could not create source code!");
}
}
如果我嘗試致電cmdSubmit_Create的點擊它不工作相同的WebMethod:
$('#cmdSubmit_Create').click(function() {
$.ajax({
type: "POST",
url: "Test.aspx/helloWorld",
data: '{"schoolId":"2236","vendor":"test","tsource":"test1234"}',
contentType: "application/json",
dataType: "json",
success: function (msg) {
alert(msg.d);
},
error: function (xhr, status, error) {
alert('error' + error)
}
});
HA!我已經有一段時間了,只需要加入return false。感謝你的幫助,以及你的幫助3nigma。非常感謝! – Alex