ASP.NET JSON web service always return the JSON response wrapped in XML

后端 未结 9 1372
清歌不尽
清歌不尽 2020-12-05 20:17

I saw a similar question but it did not resolve my issue. I have a JSON web service in an ASMX file;

The code for the web method

        [WebMethod]         


        
9条回答
  •  醉话见心
    2020-12-05 20:59

    I made the same mistake by handling the serialization myself. You re not supposed to return string but the object itself.

    If you have already developed a hundred Services like this, a work around for you: just created a aspx, ServiceCaller.aspx, remove everything in the html just leave the page directive and use the codebehind to call the service and Response.Write the string. Also take the Service method name as a parameter like "HelloWorld" and use reflection to invoke it.

    It's kinda showing your left ear with your right hand but it works. You can also easily integrate a roles management and using reflection get the service name as string check the db if the user can call this service and with reflection Invoke the method.

    (any comments on this is welcome :)

提交回复
热议问题