Returning JSON From RESTful WCF
-
I created a RESTful WCF Service and made sure that the data returned from it is of type JSON by setting its ResponseFormat property equal to WebMessageFormat.Json in my WCF Service as follows:
ResponseFormat = WebMessageFormat.Json
I checked to ensure that data returned from the WCF Service is indeed JSON by using Fiddler. What I don't understand is if the return type of the data is JSON then why do I need to convert it to a JSON string using JSON.stringify() in my jQuery AJAX call.
-
I created a RESTful WCF Service and made sure that the data returned from it is of type JSON by setting its ResponseFormat property equal to WebMessageFormat.Json in my WCF Service as follows:
ResponseFormat = WebMessageFormat.Json
I checked to ensure that data returned from the WCF Service is indeed JSON by using Fiddler. What I don't understand is if the return type of the data is JSON then why do I need to convert it to a JSON string using JSON.stringify() in my jQuery AJAX call.
First of all, you did not share the output response of the service, and we have no idea what is going wrong there. Many things can go wrong, such as the C# code might be deserializing the JSON to an object and providing you with that object. Also, the only purpose of JSON.stringify is to serialize the objects, if you are going to render the objects or their properties on the screen, there is no need to serialize the object at all. It would be a good idea to see the output of this,
success: function (response) {
alert(response); // <-- Here.
}This will either show [object Object], or some valid JSON document. That will give you an idea of the type of data that is being passed here.
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~
-
First of all, you did not share the output response of the service, and we have no idea what is going wrong there. Many things can go wrong, such as the C# code might be deserializing the JSON to an object and providing you with that object. Also, the only purpose of JSON.stringify is to serialize the objects, if you are going to render the objects or their properties on the screen, there is no need to serialize the object at all. It would be a good idea to see the output of this,
success: function (response) {
alert(response); // <-- Here.
}This will either show [object Object], or some valid JSON document. That will give you an idea of the type of data that is being passed here.
The shit I complain about It's like there ain't a cloud in the sky and it's raining out - Eminem ~! Firewall !~