Webmethod 的 AJAX 功能不起作用
我的页面上有 2 个 ajax 函数。一种在单击网格内的链接时起作用,另一种在单击按钮时起作用。网格中的那个工作得很好。然而,点击按钮的功能每次都会产生错误。
有人可以告诉我我哪里出了问题吗?我将不胜感激任何帮助。这是失败的一个:
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]
[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)
}
});
I have a 2 ajax functions on my page. One that works when a link inside a grid is clicked and one when a button is clicked. The one from the grid works perfectly. However, the function from the click of a button produces an error every time.
Can someone please tell me where i am going wrong here. I would appreciate any help. Here is the one that fails:
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);
}
});
}
The Webmethod will have more code to it but i cant seem to hit it anyway. Here is the 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!");
}
}
Also how can i go about getting the exact error that is causing this function to fail?
Thank you in advance for any help!!
ok so i figured where the problem is but i still do not have a solution. There actually wasn't anything wrong with the code. I plugged the code in at document.ready and it fired correctly. However, it will not fire correctly whenever the button is clicked. The button is nothing special either. It is a simple input button.
<input id="cmdSubmit_Create" value="Submit" type="submit" />
any ideas?
For reference i will put the code that works on the 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!");
}
}
If i try to call the same webmethod on the click of cmdSubmit_Create it doesnt work:
$('#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)
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用以下代码
http://cmsnsoftware。 blogspot.com/2011/01/how-to-call-csharp-function-in-ajax.html
use following code
http://cmsnsoftware.blogspot.com/2011/01/how-to-call-csharp-function-in-ajax.html
您的服务需要
GET
UseHttpGet = true ,而您正在做的POST
则尝试改为you service expects a
GET
UseHttpGet = true where as you are doingPOST
try instead