Emre
Emre

Reputation: 79

Internal Server Error when calling asmx web server with JSON

I have created a simple web service and published it to my host. When I call it directly via web browser, it is working.

http://test.xxxx.com/service1.asmx/HelloWorld

Then I have tried to call it from an aspx web page using the JSON response format but I'm getting an Internal Server Error.

My service:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]

[System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public  DataSet HelloWorld()
    {
        DataSet ds = new DataSet();
        //da.Fill(ds);
        ds.Tables.Add("Table0");
        ds.Tables[0].Columns.Add("Test");
        for (int i = 0; i < 20; i++)
        {
            ds.Tables[0].Rows.Add("Test " + i);
        }

        return ds;

    }
}

And I'm calling it from the aspx page with below method.

<script type="text/javascript">
    function CallService() {
        $("#lblResult").addClass("loading");
        $.ajax({
            type: "POST",
            url: "http://test.xxx.com/service1.asmx/HelloWorld",
            data: "{Lang:'tr'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: Success,
            error: Error
        });
    }

    function Success(data, status) {
        $("#lblResult").removeClass("loading");
        $("#lblResult").html(data.d);
        alert(data.d);
    }

    function Error(request, status, error) {
        $("#lblResult").removeClass("loading");
        $("#lblResult").html(request.statusText);
        alert(error);
    }

I've researched many topics related with this error but I could not find a solution.

Thanks,

Edit:

{"Message":"A circular reference was detected while serializing an object of type \u0027System.Globalization.CultureInfo\u0027.","StackTrace":"   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeCustomObject(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValueInternal(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.SerializeValue(Object o, StringBuilder sb, Int32 depth, Hashtable objectsInUse, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, StringBuilder output, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Serialization.JavaScriptSerializer.Serialize(Object obj, SerializationFormat serializationFormat)\r\n   at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n   at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

Upvotes: 2

Views: 1871

Answers (3)

Pierre
Pierre

Reputation: 9052

Check your Web.Config - Most of the issues is there.

Here is my Web.Config, works with AJAX calls to my web service as well as ASPX PAGE web methods

<system.serviceModel>
    <behaviors>
        <endpointBehaviors>
            <behavior name="MyBehaviors">
                <enableWebScript />
            </behavior>
        </endpointBehaviors>
        <serviceBehaviors>
            <behavior name="MyBehaviors">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
                <dataContractSerializer maxItemsInObjectGraph="2147483647"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <!-- I dont think the services part is necessary if you wont use Web Service, but leave it in anycase -->
    <services>
        <service name="YourNamespace.ServiceClass" behaviorConfiguration="MyBehaviors">
            <endpoint address="" behaviorConfiguration="MyBehaviors" binding="webHttpBinding" contract="YourNamespace.IServiceClass" />
        </service>
    </services>
    <bindings>
        <basicHttpBinding>
            <binding name="MyBasicHttpBinding" messageEncoding="Text" textEncoding="utf-8" allowCookies="false" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
                <readerQuotas maxStringContentLength="2147483647" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" minFreeMemoryPercentageToActivateService="1" aspNetCompatibilityEnabled="true" />
</system.serviceModel>

EDIT: Try the following Code:


Your WebMethod in *.asmx page

[WebMethod]
public DataSet HelloWorld(string Lang)
{
    DataSet ds = new DataSet();
    //da.Fill(ds);
    ds.Tables.Add("Table0");
    ds.Tables[0].Columns.Add("Test");
    for (int i = 0; i < 20; i++)
    {
        ds.Tables[0].Rows.Add("Test " + i);
    }
    return ds;
}

Ajax call in JavaScript

$.ajax({
    type: "POST",
    url: "http://test.xxx.com/service1.asmx/HelloWorld",
    data: "{'Lang':'tr'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(ds){ alert(ds.length); },
    error: function(s,a,x){ alert(a); }
});

What changed?

  • The parameters must match eachother
  • Added quotes around the parameter name in the ajax call
  • If the Data does not work correctly, try data: JSON.stringify("{'Lang':'tr'}"),

Upvotes: 0

Emre
Emre

Reputation: 79

I've changed Response format from JSON to XML, now it seems solved. I don't know why json causing this error.

//[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Xml)]

Upvotes: 3

Patrick Hofman
Patrick Hofman

Reputation: 156968

There is a topic on SO about this problem.

It seems the problem lies in the fact that there is a circular reference the built-in JSON-serializer from .NET cannot resolve. You have a few options:

  1. Use another serializer, like in the given SO topic.
  2. Return a custom business object that is not a DataSet or DataTable.
  3. Possibly, when implementing a WCF service, which is recommended anyway, the serializer behaves differently.

Upvotes: 0

Related Questions