2011-07-25 37 views
0

任何人都可以引導,下面哪些錯誤生成的JSON代碼是通過asp.net web服務的方法生成的。從asp.net Webservice生成無效的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 
--------------------------- 

回答

2

我覺得你的問題是在錯誤的請求服務結束。通過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> 

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" }; 
    } 
    } 
} 
  1. 通過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> 
    
  2. 獲取數據:請參閱工作代碼:

    的Web服務代碼

0

詭計問題?

因爲這是XML,JSON不是。

因此錯誤

+0

這是真的。但是如何在asp.net中爲JSON調用webservice? –

+0

什麼是真正奇怪的,我是,我XMLHttpRequest是返回XML,並通過JSON.parse()來與谷歌Chrome瀏覽器被成功解析,但在Android瀏覽器的股票(這我還帶領相信運行WebKit的),我得到一個「意外的令牌非法「錯誤。 它是這個原因,我認爲,如果它的工作對我的開發機瀏覽器,它會在Android瀏覽器工作了。 –

0

也許您的請求指定了「application/xml」或「text/xml」內容類型,請確保您指定的「application/json」內容類型來代替。

您可以使用調試代理像Fiddler或Web瀏覽器的開發工具(如螢火蟲,Chrome瀏覽器開發工具)驗證這一點。

如果你告訴我們的JavaScript代碼,我們可以可能找到的東西,否則請確保您使用jQuery.getJSON或指定「contentType: "application/json; charset=utf-8"」如果你使用jQuery.ajax(你需要實際的字符集)。