开发者

jquery issue always return error

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 O开发者_运维知识库nSuccess(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...


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;

    }
}


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. –


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
    });

}


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);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜