Restful WCF service not working
-
Wednesday, May 02, 2012 12:45 PMHi ,I have a restful service implemented using WCF . It's signature is[OperationContract][WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle= WebMessageBodyStyle.Bare)]List<AlbumsList_Result> GetAlbumList();I am trying to consume this service using ajax call-----------------------------------------------------------------------------------------------------------------------------------------
$(document).ready(
function () {
$(
"#btnCallRest").click(function () {
alert(
"Here");
var wcfURL = "http://localhost/RestfulMusicStore/MusicStore.svc/";
debugger;
$.ajax({
cache:
true,
url: wcfURL +
"GetAlbumList",
type:
"GET",
contentType:
"application/json; charset=utf-8",
dataType:
"json",
processdata:
true,
error:
function (msg) {
alert(
"Operation Failed!");
},
success:
function (menu) {
debugger;
alert(
"Here");
}
});
});
});-------------------------------------------------------------------------------------------This call is consistently failing and msg status is 12031. If i change reponse type to XML in WCF service , function works fine. Please let me know if I am missing anything ?ThanksSiddheshSiddhesh Sawant
All Replies
-
Wednesday, May 02, 2012 12:47 PM
Also added a tag in web.config of service
<behaviour name = "AjaxBehaviour">
<webhttp/>
</behaviour>
Siddhesh Sawant
-
Wednesday, May 02, 2012 3:23 PM
Try to inspect your request using Fiddler to look on how it looks. Also post what status code is coming up with the error. Is it 500,400, 404 etc...
Also add the below behaviour to your config as you are using json with ajax.
<behavior name="json"> <enableWebScript /> </behavior>
Rajesh S V
-
Wednesday, May 02, 2012 3:43 PM
Status code is 12031 . Response content is coming as blank in error callback.
I have also tried adding enablewebscript in binding behaviour but still same result.
Question -
I am directly using entities from edmx to return as a collection in Restful service ? Will it create a problem ?
Siddhesh Sawant
-
Wednesday, May 02, 2012 4:08 PM
I guess that your request is being processed and there is someother error on the server i.e inside your WCF service that is returning this status code.
Have you tried debugging the WCF service. Also enable tracing on your service to see what is causing the issue.
Is it that when you inspect your request using Fiddler do you see the HTTP Status code as 200?
There might be a problem if you are trying to send back a very big collection of data from the service. Hence try to increase the default values by specifying the readerQuotas. If you do have any config files please do post them.
Rajesh S V
-
Thursday, May 03, 2012 3:40 AM
Hi Rajesh ,
Thanks for response.
Strange thing is , when I change response format type to XML , I can see the results just by browsing rest URL in browser.
I have created a sample .net client aspx page and I am hitting the service by ajax call . I am not using fiddler .
Only change I have done in config is
<behaviour name = "AjaxBehaviour">
<webhttp/>
</behaviour>
and applied it to binding behaviour.
Thanks
Siddhesh Sawant
-
Thursday, May 03, 2012 9:24 AM
Hi Rajesh ,
i was able to solve the issue . I was using entity framework to get data as generic list from edmx. I have created an intermediate entity using Data Contract and mapped data returned by edmx with this entity and then returned collection as JSON . It worked !!!!
There is some issue if we directly return entities generated by edmx.
Anyways , thanks for your help.
Regards
Siddhesh
Siddhesh Sawant
- Marked As Answer by Peter pi - MSFTModerator Wednesday, May 09, 2012 1:05 AM
-
Friday, May 04, 2012 2:31 AM
Your album_listResult may not be datacontract, hence it is Serializing as a XML Serialized.
You can use DataContractJsonSerializer class to serialize as a JSON object.
Tanvir Huda Application Architect/Consultant http://thetechnocrate.wordpress.com/

