开发者

Invalid JSON Generated from asp.net Webservice?

开发者 https://www.devze.com 2023-03-23 03:10 出处:网络
Can any one guide, what is wrong with below generated JSON code which are generated thru asp.net web service\'s methods.

Can any one guide, what is wrong with below generated JSON code which are generated thru asp.net web service's methods.

---------------------------
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" };
    }
  }
}
  1. getting data via 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. getting data via 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.

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.

You can verify this using a debugging proxy like Fiddler or your web browser's development tools (e.g. Firebug, Chrome dev tools).

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"" if you're using jQuery.ajax (with the actual charset you need).

0

精彩评论

暂无评论...
验证码 换一张
取 消