views:

70

answers:

3

Hello,

I am trying to call a WCF RESTful service from jQuery. I am using JSON to encode both request and response.

The following code functions correctly in IE8:

url = 'http://ipv4.fiddler:5683/WeatherWCF/NewBinding/MyService/GetValueFloat';

$.ajax({
       url: url,
       data: '{"alias": "Udetemperatur"}',
       type: "POST",
       contentType: "application/json; charset=utf-8",
       dataType: "text",  // not "json" we'll parse
       success:
               function(res) {
                   alert('Received response: ' + res);
                   }
   });

However, in both Firefox and Chrome, res contains an empty string. After using Fiddler to monitor the request, it appears that jQuery sends an empty request to the server as shown in this screen dump: http://imgur.com/EJgwS.png
This is the successful request: http://imgur.com/S77BA.png

What am I doing wrong?

Kind regards,

Martin

A: 

http://ipv4.fiddler:5683. Due to security policies cross domain ajax requests are not allowed. In FireFox use FireBug to see exactly what's sent to the server and what's the response.

Darin Dimitrov
Turned out that XSS protection was the culprit - hosting on the same domain in IIS solved the problem.Thanks :)
Martin Wiboe
A: 

First check Darin Dimitrov's answer. Then consider using dataType: "jsonp" to make cross domain calls which return json data.

Check the jQuery.ajax documentation for more information

jitter
A: 

Try changing the name of the Url variable..

either

myurl = 'http://ipv4.fiddler:5683/WeatherWCF/NewBinding/MyService/GetValueFloat';

$.ajax({
       url: myurl ,

or put quotes around the key name ..

url = 'http://ipv4.fiddler:5683/WeatherWCF/NewBinding/MyService/GetValueFloat';

$.ajax({
       'url': url,
Gaby