0

Every time I run the ASPX page below, I receive an Internal Server Error. This is something I copied from the web & stripped the original down to this in an attempt to figure out when is causing this problem.

ASPX Page:

  <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="x.aspx.cs" Inherits="x" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title>Adventure Works</title>

       <style type="text/css">
       .loading
        {
            background-image: url('ajax-load.gif');
            background-repeat: no-repeat;
        }
       </style>

    <script src="/Scripts/jquery-1.3.2.js" type="text/javascript"></script>

    <script type="text/javascript">

        function CallService() {

            $('input[type=button]').attr('disabled', true);
            $("#CustDetails").html('');
            $("#CustDetails").addClass("loading");
            $.ajax({
                type: "POST",
                url: "GetVendor.asmx/GetVendorDetails",
                data: "{'ID': " + $("#txt_id").val() + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                error: OnError
            });

            function OnSuccess(data, status) {
                alert('success');

                $("#CustDetails").removeClass("loading");
               // $("#textVendorNameLookup").html(data.d);
                $('input[type=button]').attr('disabled', false);
            }

            function OnError(request, status, error, response) {

                $("#CustDetails").removeClass("loading");
                $("#CustDetails").html(request.statusText);
                $('input[type=button]').attr('disabled', false);

                //$("#textVendorNameLookup").html(request.statusText);
            }

        }
    </script>


</head>
<body>
    <form id="DocForm" runat="server">
    <div>

        <table style="width:31%;">
            <tr>
                <td>
                    Enter Contact ID</td>
                <td>
                    :</td>
                <td>
                    <input id="txt_id" value="12" type="text" /><input id="btnGo" type="button" 
                        value="Get Details" onclick ="CallService(); return false;"/></td>
            </tr>
            </table>
    <br />
    <div id="CustDetails" style="width: 60%; height: 75px;">
    &nbsp;
       </div>
    </div>
    </form>
</body>
</html>

ASMX page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;

/// <summary>
/// Summary description for AdvService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 

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

    public GetVendor () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    public int GetVendorDetails(int id)
    {
        return id;

    }
}

The CS page;

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

using Telerik.Web.UI;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Configuration;
using System.Reflection;


public partial class x : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

}

AHIA, Larry...

5
  • When are you getting the Server Error on page load or on the button click? Commented Jan 3, 2011 at 17:42
  • You do know that, with that asp.net page you will never get JSON returned right? only a number, that even is not formated as JSON. For that, use JSON.NET json.codeplex.com - And GetVendorDetails is not even a WebMethod !!! Commented Jan 3, 2011 at 17:45
  • @balexandre Json.net is not necessary; see my example to be posted momentarily... Commented Jan 3, 2011 at 17:52
  • It is if you start using complex objects, it is a brise to use it :) Commented Jan 3, 2011 at 17:59
  • Maaan. From what I see - You have one function inside another. Just move OnSuccess and OnFail outside. Commented Aug 12, 2011 at 16:57

4 Answers 4

1

I think you just need to decorate those methods with [WebMethod] like so:

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

    [WebMethod]
    public int GetVendorDetails(int id)
    {
        return id;

    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

chprpipr, I added [WebMethod] & I get the same result. Any other ideas?? I am a newbie to jQuery, so I am grasping @ straws.
there is no real error, it returns 'Internal Server Error' I have taken some advice from below & used the 'Inspect Element' in Chrome & found that the data parameter where #txt_id was supposed to be populated is not.("Message":"Invalid web service call, missing value for parameter:)
@larryr inspect the variable request.responseText in your OnError method, you will get what ASP.NET outputs (the yellow page of death)
@larryr: That is the real error. It looks like you've got it nailed. Once you supply a valid input, it should work. Yeah, I pretty much develop with Firebug open all the time so I can watch the back and forth between my code and the server. It really helps to demystify what jQuery is doing.
@chprpipr - should not $("#txt_id").val() grab the value from the text box named 'txt_id'?? It obviously is not!! Any idea why???
|
1

OK, the answer to this one is a 'boot to the head'... when you specify the data pair, the name has to be the same case in the data as well as in the web service.... Thanks everyone for all the help/suggestions. –

Comments

1

Try this it should work,

function CallService() {

    var CatID = {"ID": $('#txt_id').val()}
    $('input[type=button]').attr('disabled', true);
    $("#CustDetails").html('');
    $("#CustDetails").addClass("loading");
    $.ajax({
        type: "POST",
        url: "AdvService.asmx/GetCtcDetails",
        data: JSON.stringify(CatID ),,
        contentType: "application/json; charset=utf-8",
        dataType: "json",         

        success: OnSuccess,
        error: OnError
    });

}

Comments

0

You can use VS.net's debugger to see what code is running (if any) when an ajax call is invoked. This is a good place to start for "Internal Server Error" ajax problems.

Next, if you aren't using Firebug (a Firefox plug in for web development) you should definitely check it out to help with ajax call debugging among much more. More specific to your inquiry, see if changing your GetVendorDetails method to be more like this helps.

using System.Web.Script.Services;
using System.Web.Services;
using System.Web.Script.Serialization;

...

[WebMethod]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
public string GetVendorDetails(string Id) {
  // do your processing here to get your result
  return new JavaScriptSerializer().Serialize(yourObjectInstanceToBeJsonSerialized);
}

2 Comments

you do realize that even with that, ASMX Service outputs ALWAYS XML, right? to return pure JSON either u use WCF of a wrapper call.
@balexandre, note the ResponseFormat.Json decorator; it returns Json to my $.ajax jquery calls, enough said

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.