[英]Invalid JSON Generated from asp.net Webservice?
Can any one guide, what is wrong with below generated JSON code which are generated thru asp.net web service's methods. 任何一个指南,下面生成的JSON代码有什么问题,这些代码是通过asp.net web服务的方法生成的。
---------------------------
Message from webpage
---------------------------
Error: Invalid JSON: <?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">[{"id:" '1',"title:" 'Event1',"start:" 1310236200,"end:" 1310236200,"allDay:"true,"description:" 'Event1'},{"id:" '3',"title:" 'Event2',"start:" 1309804200,"end:" 1309804200,"allDay:"true,"description:" 'Event2'},{"id:" '4',"title:" 'Event5',"start:" 1311705000,"end:" 1311705000,"allDay:"true,"description:" 'Event5'},{"id:" '5',"title:" 'Event3',"start:" 1309006800,"end:" 1309006800,"allDay:"false,"description:" 'Event3'},{"id:" '6',"title:" 'Event4',"start:" 1310495400,"end:" 1310495400,"allDay:"true,"description:" 'Event4'},{"id:" '7',"title:" 'Time Event1',"start:" 1312144200,"end:" 1312174800,"allDay:"false,"description:" 'Time Event1'},{"id:" '8',"title:" 'save1',"start:" 1312309800,"end:" 1312309800,"allDay:"true,"description:" 'save1111'},{"id:" '9',"title:" 'today',"start:" 1311273000,"end:" 1311273000,"allDay:"true,"description:" 'today'}]</string>
---------------------------
OK
---------------------------
I think your problem is concluded in wrong request to service. 我认为你的问题是在错误的服务请求中得出的。 Please see working code:
请参阅工作代码:
Web-service code: 网络服务代码:
namespace Test.Service
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
public class WebService1 : WebService
{
[WebMethod]
public object HelloWorld()
{
// ! return anonymous object. It cannot be serialized to xml and orients solely to json-request.
return new { value = 12345, name = "John" };
}
}
}
getting data via jquery 通过jquery获取数据
<script type="text/javascript"> $(document).ready(function () { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Service/WebService1.asmx/HelloWorld", data: "{}", dataType: "json", success: function (data) { alert(data.d.value); alert(data.d.name); } }); } </script>
getting data via ASP.NET AJAX 通过ASP.NET AJAX获取数据
<asp:ScriptManager ID="_scriptManager" runat="server"> <Services> <asp:ServiceReference Path="Service/WebService1.asmx" /> </Services> </asp:ScriptManager> <script type="text/javascript"> $(document).ready(function () { Test.Service.WebService1.HelloWorld(OnComplete); function OnComplete(result) { alert(result.value); alert(result.name); } } </script>
Trick question? 招数问题?
Because that's XML, not JSON. 因为那是XML,而不是JSON。
hence the error 因此错误
Perhaps your request specifies the " application/xml
" or " text/xml
" content type, make sure you specify the " application/json
" content type instead. 也许您的请求指定了“
application/xml
”或“ text/xml
”内容类型,请确保您指定“ application/json
”内容类型。
You can verify this using a debugging proxy like Fiddler or your web browser's development tools (eg Firebug, Chrome dev tools). 您可以使用调试代理(如Fiddler)或Web浏览器的开发工具(例如Firebug,Chrome开发工具)来验证这一点。
If you show us the JavaScript code we can possibly find something, otherwise make sure you're using jQuery.getJSON or specifying " contentType: "application/json; charset=utf-8"
如果您向我们展示JavaScript代码,我们可能会找到一些东西,否则请确保您使用的是jQuery.getJSON或指定“
contentType: "application/json; charset=utf-8"
contentType: "application/json; charset=utf-8"
" if you're using jQuery.ajax (with the actual charset you need). contentType: "application/json; charset=utf-8"
”如果您正在使用jQuery.ajax (使用您需要的实际字符集)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.