为什么我的ajax呼叫不起作用? [英] Why isn't my ajax call working?
问题描述
大家好。
承认我不熟悉客户端编码..
我想让这个ajax调用工作,但我可以弄清楚..
Hi guys.
Admitting that I'm not familiar with client-side coding..
I'm trying to get this ajax call working, but I can't figure it out..
function ShowAlert()
{
$.ajax(
{
type: "POST",
url: "DownladData.aspx/Alert",
data: "{}",
contentType: "application/json; charset=utf-8",
datatype: "json",
statusCode: {
404: function ()
{
alert('Method not found!');
}
},
success: function (msg) {
alert('Everything is ok!');
},
error: function (msg) {
alert('Error!!!')
}
});
}
虽然这是C#代码
While this is the C# code
[WebMethod]
public static void Alert()
{
}
这是我通过该按钮进行ajax调用
And this is the button through which I make the ajax call
<input type="button" runat="server" önclick="ShowAlert();" value="Alert" />
此例程不会执行anithing,但我会除了一个警告,其中一切都是好消息,而不是错误!!!。
使用chrome调试器,我收到此消息:
无法加载资源:服务器响应状态为404(未找到)http://localhost/AdminPages/DownloadData.aspx/Alert
其中Alert()是DownloadData.aspx.cs文件中的WebMethod。
有什么建议吗?
非常感谢!
This routine doesn't do anithing, but I'd except an alert with the Everything is ok message, instead of an Error!!!.
Using chrome debugger, I get this message:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost/AdminPages/DownloadData.aspx/Alert
where Alert() is the WebMethod in the DownloadData.aspx.cs file.
Any suggestion?
Thank you very much!
推荐答案
.ajax(
{
type: POST,
url:DownladData.aspx / Alert,
data: {},
contentType: 应用/ JSON; charset = utf-8,
数据类型: json,
statusCode:{
404 : function ()
{
alert(' 找不到方法!');
}
} ,
成功: function (msg){
alert(' 一切都好!');
},
错误: function (msg){
alert(' 错误!!!')
}
} );
}
.ajax( { type: "POST", url: "DownladData.aspx/Alert", data: "{}", contentType: "application/json; charset=utf-8", datatype: "json", statusCode: { 404: function () { alert('Method not found!'); } }, success: function (msg) { alert('Everything is ok!'); }, error: function (msg) { alert('Error!!!') } }); }
虽然这是C#代码
While this is the C# code
[WebMethod]
public static void Alert()
{
}
这是按钮我做了ajax调用
And this is the button through which I make the ajax call
<input type="button" runat="server" önclick="ShowAlert();" value="Alert" />
这个例程没有不要做anithing,但我除了一个警告,其中一切都是好消息,而不是错误!!!。
使用chrome调试器,我得到这个消息:
无法加载资源:服务器响应状态为404(未找到)http://localhost/AdminPages/DownloadData.aspx/Alert
其中Alert()是DownloadData.aspx.cs文件中的WebMethod。
有什么建议吗?
非常感谢!
This routine doesn't do anithing, but I'd except an alert with the Everything is ok message, instead of an Error!!!.
Using chrome debugger, I get this message:
Failed to load resource: the server responded with a status of 404 (Not Found) http://localhost/AdminPages/DownloadData.aspx/Alert
where Alert() is the WebMethod in the DownloadData.aspx.cs file.
Any suggestion?
Thank you very much!
好吧,我找到了这个难题的解决方案。
我会报告我发现了什么来帮助所有其他人。
我的Ajax / Jquery代码没问题。
方法必须放在 .aspx页面的代码隐藏中(不确定它是否正常工作,即使放在母版页后面)它必须声明为 p ublic 。
您可以在通过jquery / ajax调用的aspx.cs方法中调用另一个方法,就像调用任何其他外部dll调用一样。
感谢所有人的帮助。
Well, I've found the solution to this puzzle.
I'll report what I've discovered to help all of the other people.
My Ajax/Jquery code was ok.
The method must be put in the codebehind of an .aspx page (not sure if it's working even if put behind a master page) and it has to be declared as public.
You can call another method from within the aspx.cs method called via jquery/ajax, as you would do with any other external dll call.
Thx everybody for your help.
我的猜测是您的错误处理错误。我不熟悉statusCode选项(可能是最近添加的?)
这是我检查错误状态的方法:
My guess would be that your error handling is wrong. I'm not familiar with statusCode option (maybe it was added recently?)
This is how I would check for the error status:
.
.
.
error: function (xhr, status, msg)
(jqXHR.status && xhr.status == 404){
alert(xhr.responseText); /* or your custom text*/
}else{
alert("Something else went wrong");
}
几种变体甚至错误地图在其中 [ ^ ]
如果这有帮助,请接受解决方案,以便其他人可以找到它。
Several variations and even error maps here[^]
If this helps, please accept the solution so other may find it.
这篇关于为什么我的ajax呼叫不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!