Javascript 从asp.net Web服务生成的JSON无效?
下面通过asp.net web服务的方法生成的JSON代码有什么问题Javascript 从asp.net Web服务生成的JSON无效?,javascript,jquery,asp.net,fullcalendar,Javascript,Jquery,Asp.net,Fullcalendar,下面通过asp.net web服务的方法生成的JSON代码有什么问题 --------------------------- Message from webpage --------------------------- Error: Invalid JSON: <?xml version="1.0" encoding="utf-8"?> <string xmlns="http://tempuri.org/">[{"id:" '1',"title:" 'Event1'
---------------------------
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
---------------------------
---------------------------
来自网页的消息
---------------------------
错误:无效的JSON:
[{id:'1','title:'Event1','start:'1310236200',end:'1310236200',all day:'true',description:'Event1'},{id:'3','title:'Event2','start:'1309804200','start:'true','Event2'},{id:'4','title:'Event5',start:'1311705000',all day:'true',description:'Event5','id:'5','title:'“事件3”,“开始:”1309006800,“结束:”1309006800,“全天:”false,“描述:”'Event3'},{id:'6',“标题:'Event4',“开始:”1310495400,“结束:”1310495400,“全天:”true,“描述:'Event4'},{id:'7',“标题:'TimeEvent1',“开始:”1312144200,“结束:”1312174800,“全天:”false,“描述:'TimeEvent1'},{id:'8',“标题:'save1',“开始:”1312309800,“结束:”1312309800,“全天:”真,“描述:”'save1111'},{“id:'9',“标题:'today',“开始:”1311273000,“结束:”1311273000,“全天:”真,“描述:'today'}]
---------------------------
好啊
---------------------------
技巧问题
因为这是XML,不是JSON
因此出现了错误我认为您的问题是在错误的服务请求中得出的结论。请参阅工作代码: 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" };
}
}
}
<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>
$(文档).ready(函数(){
$.ajax({
类型:“POST”,
contentType:“应用程序/json;字符集=utf-8”,
url:“Service/WebService1.asmx/HelloWorld”,
数据:“{}”,
数据类型:“json”,
成功:功能(数据){
警报(数据d值);
警报(data.d.name);
}
});
}
<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>
$(文档).ready(函数(){
Test.Service.WebService1.HelloWorld(OnComplete);
函数完成(结果){
警报(结果值);
警报(result.name);
}
}
也许您的请求指定了“
application/xml
”或“text/xml
”内容类型,请确保您指定了“application/json
”内容类型
您可以使用调试代理或web浏览器的开发工具(例如Firebug、Chrome开发工具)来验证这一点
如果您向我们展示JavaScript代码,我们可能会找到一些东西,否则请确保您正在使用或指定“
contentType:”application/json;charset=utf-8”
”,如果您正在使用(使用您需要的实际字符集).thts true。但是如何在asp.net中为JSON调用webservice?对我来说真正奇怪的是,我的XmlHttpRequest返回XML并通过JSON.parse()与Google Chrome成功解析,但在Android股票浏览器(我相信它也在WebKit上运行)上,我得到了一个“意外的非法令牌”错误。正是因为这个原因,我认为如果它能在我的开发者机器浏览器上工作,它也能在Android浏览器上工作。