使用JSON调用asmx Web服务器时发生内部服务器错误
我创建了一个简单的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"}
有a topic on SO这个问题。
看来问题在于,事实上有一个来自.NET的内置JSON序列化程序无法解决的循环引用。您有几种选择:
- 使用另一个串行,像在给定的SO话题。
- 返回不是DataSet或DataTable的自定义业务对象。
- 可能的是,在实现推荐的WCF服务时,序列化程序的行为会有所不同。
返回一个字符串也解决了JSON响应中的问题。你知道为什么要返回一个导致这个错误的数据集,有没有选择发布数据集? – Emre
我认为json序列化程序在走下依赖关系时并不那么聪明。我认为它一直沿着相关的对象走下去(例如,父>子>父)。 XML序列化程序的工作方式不同,在这种情况下可能会避免提到的问题。 –
我已经将JSON的响应格式更改为XML,现在看来已经解决了。 我不知道为什么json导致这个错误。
//[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Xml)]
检查您的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'}"),
OP使用ASMX webservices,而不是WCF。 –
正如我所说,上面的配置也适用于普通ASPX页面上的WebMethods。所以我不认为在ASMX页面上工作应该是一个问题。 – Pierre
system.serviceModel配置部分仅用于WCF –
请加满错误细节 – geedubb
它看起来像你正在开始一个新项目。使用WCF。 ASMX已弃用,不应再使用。 –
@geedubb我在第一篇文章中添加了错误细节, – Emre