2014-01-19 150 views
2

我創建了一個簡單的Web服務並將其發佈到我的主機。 當我通過網絡瀏覽器直接調用它時,它正在工作。使用JSON調用asmx Web服務器時發生內部服務器錯誤

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

然後我試圖使用JSON響應格式的ASPX頁面調用它,但我得到一個內部服務器錯誤。

我的服務:

[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; 

    } 
} 

而且我從下方的方法aspx頁面調用它。

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

我已經研究了許多與此錯誤相關的主題,但我找不到解決方案。

感謝,

編輯:

{"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"} 
+0

請加滿錯誤細節 – geedubb

+0

它看起來像你正在開始一個新項目。使用WCF。 ASMX已棄用,不應再使用。 –

+0

@geedubb我在第一篇文章中添加了錯誤細節, – Emre

回答

0

a topic on SO這個問題。

看來問題在於,事實上有一個來自.NET的內置JSON序列化程序無法解決的循環引用。您有幾種選擇:

  1. 使用另一個串行,像在給定的SO話題。
  2. 返回不是DataSet或DataTable的自定義業務對象。
  3. 可能的是,在實現推薦的WCF服務時,序列化程序的行爲會有所不同。
+0

返回一個字符串也解決了JSON響應中的問題。你知道爲什麼要返回一個導致這個錯誤的數據集,有沒有選擇發佈數據集? – Emre

+0

我認爲json序列化程序在走下依賴關係時並不那麼聰明。我認爲它一直沿着相關的對象走下去(例如,父>子>父)。 XML序列化程序的工作方式不同,在這種情況下可能會避免提到的問題。 –

3

我已經將JSON的響應格式更改爲XML,現在看來已經解決了。 我不知道爲什麼json導致這個錯誤。

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

檢查您的Web.Config - 大部分問題都在那裏。

這裏是我的web.config,工程用AJAX調用我的web服務以及ASPX頁面Web方法

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

編輯:試試下面的代碼:


您的WebMethod在*.asmx頁面

[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調用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); } 
}); 

是什麼改變了?

  • 參數必須海誓山盟
  • 周圍的參數名稱加引號的Ajax調用
  • 如果數據不能正常工作,嘗試匹配data: JSON.stringify("{'Lang':'tr'}"),
+0

OP使用ASMX webservices,而不是WCF。 –

+0

正如我所說,上面的配置也適用於普通ASPX頁面上的WebMethods。所以我不認爲在ASMX頁面上工作應該是一個問題。 – Pierre

+0

system.serviceModel配置部分僅用於WCF –

相關問題