简体   繁体   English

jQuery Ajax不会触发

[英]jquery ajax doesn't fire

I am trying to call a server-side function using Jquery ajax, it doesn't work, I get no error. 我正在尝试使用Jquery ajax调用服务器端函数,它不起作用,没有错误。 what can be the problem ? 可能是什么问题? is there a set of rules to make sure my $ajax will work? 有一套规则来确保我的$ ajax可以工作吗?

//HTML // HTML

 <asp:DropDownList ID="DDL" runat="server">
                  <asp:ListItem>aaa</asp:ListItem>
                    <asp:ListItem>bbb</asp:ListItem>
                </asp:DropDownList>

//JS // JS

 $(document).ready(function () {
            $("#DDL").change(function () {                             
                $.ajax({
                    type: "POST",
                    url: "signToCity.aspx/getStreets",
                    contentType: "application/json; charset=utf-8"              
                });

            });
        });

//Serverside //服务器端

  [WebMethod]
        public static void getStreets()
        {                       
         string test="no return:just checking by breakpoint if function is working."

        }

Here are the detail of rules to make a AJAX Call : See Here in detail 这是进行AJAX调用的规则的详细信息: 请参阅此处详细信息

$.ajax({
   url: 'http://api.joind.in/v2.1/talks/10889',
   data: {
      format: 'json'
   },
   error: function() {
      $('#info').html('<p>An error has occurred</p>');
   },
   dataType: 'jsonp',
   success: function(data) {
      var $title = $('<h1>').text(data.talks[0].talk_title);
      var $description = $('<p>').text(data.talks[0].talk_description);
      $('#info')
         .append($title)
         .append($description);
   },
   type: 'GET'
});

Change your server side code to the following: (change void to string and return value) 将您的服务器端代码更改为以下内容:(将void更改为string并返回值)

c# C#

[WebMethod]
public static string getStreets()
{
    return "no return:just checking by breakpoint if function is working.";
}

jQuery: (add handlers for ajax response) jQuery :(为Ajax响应添加处理程序)

$.ajax({
    type: "POST",
    url: "signToCity.aspx/getStreets",
    contentType: "application/json; charset=utf-8",
    success: function (response) {
        console.log(response.d);
    },
    error: function (response) {
        console.log('error ' + JSON.stringify(response));
    }
});

Are you setting the clientidmode to static for your web control somewhere? 您是否正在将Web控件的clientidmode设置为static If not, ASP.NET renders it with an entirely different id 如果没有,ASP.NET将使用完全不同的ID呈现它

You labeled the follwing as HTML: 您将以下标记为HTML:

<asp:DropDownList ID="DDL" runat="server">
                  <asp:ListItem>aaa</asp:ListItem>
                    <asp:ListItem>bbb</asp:ListItem>
                </asp:DropDownList>

but it is not HTML, it is your aspx page that will be processed and output html. 但是它不是HTML,而是将处理并输出html的aspx页面。 Look at the page source and see the actual HTML that is rendered. 查看页面源代码,并查看呈现的实际HTML You will notice your dropdown list is a select element with an id something like ctl00$ContentPlaceHolder1$Page$#DDL 您会注意到您的下拉列表是一个select元素,其id类似于ctl00$ContentPlaceHolder1$Page$#DDL

Your jQuery cant find $("#DDL") because it doesnt exist. 您的jQuery无法找到$("#DDL")因为它不存在。

Option 1: 选项1:

If your javascript is directly on the .aspx page and not external, you can use the following to print the generated client id to your page 如果您的JavaScript直接位于.aspx页面上而不位于外部,则可以使用以下命令将生成的客户端ID打印到页面上

 ...$("#<%=DDL.ClientID%>").change(function () {                             
                $.ajax({...

Option 2: 选项2:

Your other option is to set the ClientIDMode of your DropDownList to static , so it doesn't change when the page is rendered 您的另一个选择是将DropDownList的ClientIDMode设置为static ,因此呈现页面时它不会改变

<asp:DropDownList ID="DDL" runat="server" ClientIDMode="static">
                  <asp:ListItem>aaa</asp:ListItem>
                    <asp:ListItem>bbb</asp:ListItem>
                </asp:DropDownList>

Could your url be the problem? 您的网址可能是问题吗? signToCity.aspx/getStreets doesn't look right. signToCity.aspx/getStreets看起来不正确。 Perhaps it's supposed to be signToCity/getStreets.aspx ? 也许应该是signToCity/getStreets.aspx

Or even just signToCity/getStreets ? 甚至只是signToCity/getStreets吗?

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

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