3

i can't figure out what i'm missing, is there an IIS Setting that needs to be changed, possibly?

$(document).ready(function () {

    function AjaxSuccess(result) {

        alert('result: ' + result);
    }

    function AjaxError(result) {
        alert("error");
        alert(result.status + ' ' + result.statusText);
    }


    $(".hlp").click(function () {
        var myVal= $(this).val();
        var id = $(this).attr('id');


        $.ajax({
            type: "POST",
            url: "AjaxWebMethods.aspx/GetHelpText",
            contentType: "application/json; charset=utf-8",
            data: "{'helpText' : '" + id + "'}",
            dataType: "json",
            success: AjaxSuccess,
            error: AjaxError
        });
    });

});

my web method is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AjaxWebMethods : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e){ }

    #region Exposed WebMethods
    [System.Web.Services.WebMethod()]
    public string GetHelpText(string helpItem)
    {
        string helpText = "testing web method";
        return helpText;
    }
    #endregion
}

i keep getting 2 pop ups "error" and then the 501 error. please help me figure this out.

Madam Zu Zu
  • 6,437
  • 19
  • 83
  • 129

2 Answers2

4

you need to change this line data: "{'id' : '" + id + "'}", to data: "{'helpItem' : '" + id + "'}",

as webmethod is taking helpItem as parameter name.

so ajax function finally wil be

$.ajax({
            type: "POST",
            url: "AjaxWebMethods.aspx/GetHelpText",
            contentType: "application/json; charset=utf-8",
            data: "{'helpItem' : '" + id + "'}",
            dataType: "json",
            success: AjaxSuccess,
            error: AjaxError
        });

and make your serverside method static like this

[System.Web.Services.WebMethod()]
    public static string GetHelpText(string helpItem)
    {

Check the article help you for this : Calling Server Side function from Client Side Script

Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
3

Your data is id and your Web Method is expecting helpItem. Try aligning the two.

data: "{helpItem: '" + id + "'}"

Another thing to try is to add static to your WebMethod declaration.

[System.Web.Services.WebMethod()]
public static string GetHelpText(int helpItem)
{
Neil Knight
  • 47,437
  • 25
  • 129
  • 188