我在我的页面上有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