Search code examples
c#jqueryasp.netwebmethodiis-5

500 Internal Error on jQuery Web Method Call - IIS 5.1 ( :-/ yea i know...)


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.


Solution

  • 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