How to identify the JSON response?
I am developing the REST enabled WCF Service. I am using the following code inside the interface.
[OperationContract]
//[WebGet]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
List<String> GetProjects();
I want the method should return the JSON response. I am passing the parameter through the URL as follows.
http://localhost:51565/RestWebService/Search.svc/GetProjects
Now when I use the above URL in the address bar, the browser ask me for downloading the file. I am new the REST web service & also JSON. I am not aware whether in the above case I am getting the JSON response or something else ? How can I identify开发者_如何学Python that the above response is the JSON response ?
Most of the the current browsers do not render Json when they see the media-type application/json. You will run into this problem with many media types. My suggestion is to just stop trying to debug using a web browser.
Install fiddler. It will save you.
Fiddler is a debugging tool for working with HTTP. You will be able to see exactly what is being transmitted to and from your service and you will be able to create POST requests in order to test your service.
It does take a bit of time to get used to but it is well worth it if you are doing any amount of work with HTTP.
Download file and look inside if you have a valid json object. Eventually parse using jQuery.parseJSON.
Browser propmts you to download because it received a content type is does not understand, does not have too much to do with content. It is not the responsability of the browser to decide if its a valid json but of the calling code.
I second fiddler for its JSON support, but for instances where you can call the service with a GET request then Chrome will display the result in the browser just fine.
Just copied this out a Chrome tab after calling a WCF service
{"GetDetailResult":{"Address":null,"MainPhotoURL":null,"Photos":[]}}
精彩评论