John O
John O

Reputation: 942

No response from WCF Service called via AJAX

I know this question has been asked many times, but I have not been able to find any solution to my particular problem.

I have a WCF web service using webHttpBinding that I am trying to call from a web page via a jquery $.ajax() call.

serviceFailed = function (a, b, c) {
    alert('Failure: ' + c);
}

$.ajax({
    type: "GET",
    url: "http://localhost/KasraNet.Services.Centurian.WebService/CenturianAdmin.svc/bob",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    processdata: true,
    error: serviceFailed,
    success: function (msg) {
        alert('Success: ' + msg);
    }
});

When run, the success alert is fired, but the msg is null.

Using FireBug, I can see that the response is completely empty, whilst using HttpFox, the error NS_ERROR_DOM_BAD_URI is returned. I cant see how this is a cross browser issue as I'm going to localhost and the service is also running on the same machine (I have even tried to change localhost to the machine name without success).

The service is defined thus.

<OperationContract(), DataContractFormat()>
<WebInvoke(BodyStyle:=WebMessageBodyStyle.Wrapped, Method:="GET", ResponseFormat:=WebMessageFormat.Json)> _
Function bob() As String

The Request and Response headers for the ajax call are

Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.2.17) Gecko/20110420 Firefox/3.6.17 (.NET CLR 3.5.30729)
Accept: application/json, text/javascript, */*; q=0.01
Accept-Language: en-gb,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Content-Type: application/json; charset=utf-8
Origin: null

Server: Microsoft-IIS/5.1
Date: Fri, 13 May 2011 14:20:53 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
Content-Length: 19
Cache-Control: private
Content-Type: application/json; charset=utf-8

Any help would be greatly appreciated - I'm really starting to pull my hair out!

EDIT: Now what is interesting is if I paste the url into the browser address bar, a download is initiated with the results of the service method in json format.

Thanks John

Upvotes: 1

Views: 1706

Answers (2)

John O
John O

Reputation: 942

Ok, I am a bit of a numb skull.

I got thinking about the cross-domain issue and realised I was opening a direct link to the file and not going through IIS.

Creating a VD and opening the page through this, the results I was expecting came back.

Thankyou for taking the time to look at this issue, and sorry for wasting your time.

Upvotes: 1

Codo
Codo

Reputation: 78795

The data in your request is most likely the problem:

  • Your WCF function doesn't expect any input data.

  • Since you pass a string as the data (it looks like an empty JSON object but it's in fact a string), jQuery will append it to the URL like this: http://localhost/KasraNet.Services.Centurian.WebService/CenturianAdmin.svc/bob?{} But that's an invalid URL and IIS will reject it.

Upvotes: 0

Related Questions