简体   繁体   English

无法使用jQuery Ajax调用Page WebMethod?

[英]Cannot make a call to Page WebMethod using jQuery Ajax?

I am currently trying to develop a chat applicaiton in asp.net2.0 and requires me to make a call to aspx Page Webmethod .My issue is that i cannot make a call to the page webmethod. 我目前正在尝试在asp.net2.0中开发一个聊天应用程序,并要求我致电aspx Page Webmethod我的问题是我无法致电该页面webmethod。

Not showing any message in error console of ff,so cannot findout where i have gone wrong.There is no issue while making a call webservice. 在ff的错误控制台中未显示任何消息,因此无法找到我哪里出错了。拨打网络服务时没有问题。

Heres the code i have writtern 这是我写的代码

 $(document).ready(function(){
   $("#btnSend").click(function(){
     $.ajax({
      type:"POST",
      data:"{}",
      url:"Chat.aspx/GetTime",
      contentType:"application/json; charset=utf-8",
      dataType:"json",
      success:function(msg){
        alert(msg.d);
      },
      error:function(msg){
        alert(msg.d);
      }
   });

  });

});

This is my chat.aspx page.The GetTime function is not getting called?Is the issue with the url ?? 这是我的chat.aspx PAGE电泳GETTIME功能是没有得到所谓的?是用URL中的问题?

[WebMethod] 
public static string GetTime()
{
   return DateTime.Now.ToString();
}

Three things I would look at are: 我要看的三件事是:

1.) I'm wasn't sure that ASP.NET 2.0 could use the JSON AJAX stuff. 1.)我不确定ASP.NET 2.0是否可以使用JSON AJAX。 I thought that came about in 3.5, but may be thinking of the ASP.NET AJAX implementation instead of jQuery's. 我认为这是在3.5中实现的,但可能是考虑使用ASP.NET AJAX实现而不是jQuery的实现。 See: Jquery's Ajax Property For Asp.Net 2.0 , specifically: 请参阅: Asp.Net 2.0的Jquery的Ajax属性 ,特别是:

You need to add this attribute to your webserver class 您需要将此属性添加到您的网络服务器类

[System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService

and this attribute to your functions 这是你的功能

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

2.) You may want to check to see if the version of jQuery AJAX you are using is compatible with ASP.NET 2.0. 2.)您可能想检查一下您使用的jQuery AJAX版本是否与ASP.NET 2.0兼容。 I remember there being an issue there and needing to use older versions of jQuery (like 1.3) See: http://forum.jquery.com/topic/jquery-ajax-not-working-for-asp-net-2-0-7-5-2011 我记得那里存在一个问题,需要使用旧版本的jQuery(如1.3),请参阅: http : //forum.jquery.com/topic/jquery-ajax-not-working-for-asp-net-2-0 -7-5-2011

3.) This may be a long shot but is your page called Chat.aspx, or chat.aspx? 3)这可能是一个长镜头,但是您的页面名为Chat.aspx还是chat.aspx? Javascript capitalization matters (O; JavaScript大写很重要(O;

Long shots, all of them, so good luck (: 远射,所有人,祝你好运(:

[EDIT] Was just rereading the question and another idea popped in: Is the button from your .ASPX page? [编辑]只是在重读问题,然后出现另一个想法:.ASPX页面上的按钮吗? The reason I ask is because it's possible that when it's getting rendered on the page your button is no longer 'ID="btnSend"' but instead 'ID="ctl_btnSend_0897234h15807"' or so. 我问的原因是,当它在页面上呈现时,您的按钮可能不再是“ ID =“ btnSend””,而是“ ID =“ ctl_btnSend_0897234h15807””左右。 If you have attached your jQuery to find '#btnSend' there's a chance that it's not the same ID in the DOM anymore. 如果您已将jQuery附加到“ #btnSend”,则有可能该DOM中的ID不再相同。

Hoping that helps![/EDIT] 希望能有所帮助![/ EDIT]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM