从asp.net Webservice生成的无效JSON
Invalid JSON Generated from asp.net Webservice?
有谁能告诉我,下面通过asp.net web service的方法生成的JSON代码有什么问题吗?
---------------------------
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
---------------------------
我认为你的问题归结于错误的服务请求。请参阅工作代码:
web服务代码:
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" };
}
}
}
-
通过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>
-
通过ASP获取数据。净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>
难题?
因为那是XML,不是JSON。
因此错误
也许您的请求指定了" application/xml
"或" text/xml
"内容类型,请确保您指定了" application/json
"内容类型。
您可以使用调试代理,如Fiddler或您的web浏览器的开发工具(如Firebug, Chrome开发工具)来验证这一点。
如果你向我们展示JavaScript代码,我们可能会找到一些东西,否则确保你使用jQuery。如果使用jQuery,则指定"contentType: "application/json; charset=utf-8"
"。Ajax(使用您需要的实际字符集)。
相关文章:
- Jquery Datatables错误:无效的JSON基元:draw
- Laravel数据表无效的JSON响应
- 使用JSON.parse时出现无效字符javascript错误
- JSON.stringify抛出RangeError:大型对象的字符串长度无效
- 解析 JSON 时出现无效字符错误
- 为什么 JSON.parse 给出无效的字符
- TypeError: 使用 Jquery JSON 获取数据时无效的 'in' 操作数 obj
- AJAX:无效的JSON响应
- 为什么MooTools JSON.encode()返回“$呼叫者”:无效的
- JSON.parse中存在无效字符
- 无效的JSON-试图嵌套数组
- JSON无效字符错误
- 修复无效JSON的最有效方法
- 无效的“in”操作数 obj JQUERY - 尝试从 JSON 获取数据时出现类型错误
- 用于修复无效 JSON 的正则表达式
- 为什么这是谷歌图表的无效JSON字符串
- 来自json_encode()的无效json
- 从asp.net Webservice生成的无效JSON
- 如何正确使用eval()来解析无效Json
- iccast json-status.xls,无效json答案与空白歌曲标题