开发者

Why is ajax request not working?

开发者 https://www.devze.com 2023-02-20 02:52 出处:网络
I have been having a hell of a time with asp.net and ajax request using JQuery. I cannot figure out why the following does not work. Am I missing something? It seems to fire off the request ok (no err

I have been having a hell of a time with asp.net and ajax request using JQuery. I cannot figure out why the following does not work. Am I missing something? It seems to fire off the request ok (no errors), but it never goes inside the method.

 // asp.net webforms (NOT MVC)
public partial class PollManager : System.Web.UI.Page
{

        [WebMethod]
        [ScriptMethod]
        public static string DeletePoll(string pollId)
        {
            string test = "testing";

            return test;
        }
}


  **Updated Code**

  $.ajax({
       开发者_开发技巧 url: "PollManager.aspx/DeletePoll",
        data: { "pollId": "17" },
        dataType: "text",
        success: function (data)
        {
            alert(data);
        }                
    });

In the master page there is a <asp:toolkitscriptmanager>, I'm not sure if that makes any difference.


Well... for one data: "17" isn't going to work. You'd need to do something like data: {"pollId" : "17"}


It seems like you are mixing up ASP.NET web services, and ASP.NET MVC routes. There's no way that the ASP.NET web forms runtime (ASPX) would know how to take your data (17) and map it to the pollId parameter.


You need to add a success handler to your options hash that are passed into $.ajax, something like this:

    $.ajax({
        url: "PollManager.aspx/DeletePoll",
        data: "17",
        dataType: "text",
        success: function(data) {
            alert(data);
        }             
    });


I think you need to remove [ScriptMethod], Or change it to [ScriptMethod(ResponseFormat=ResponseFormat.Json)] because you don't want XML formatted response. You want JSON response, don't you?

Also I don't know how it serializes a string object, but you might want to also try returning an object that you define. Like new SomeResponseObject(){ MyResponseText="some text" }

Then if you return that object, in javaScript you should be able to access it like this data.MyResponseText

0

精彩评论

暂无评论...
验证码 换一张
取 消