3

I'm making the following call to a webservice:

$.ajax({
    dataType:'json',
    cache:false,
    type: "GET",
    url: url,
    success: function (data) {
        alert("success");            
    },
    error: function (xhr, ajaxOptions, thrownError){
        alert('Failed to subscribe.');
    }  
});                

The webservice is hit and definitely returns json - I can hit it via the browser and get what I expect. In my site, the error function is always called.

using Fiddler I can see there is a 200 result - the only thing I notice is that in the response fiddler says

Response is encoded and may require decoding before inspection. Click here to transform.

When I click it, the response goes from being a load of random symbols to being my expected json.

Upon Googling this, I see suggestions of adding contentType: "application/json;charset=UTF-8", to my call.

This stops my webservice function from being hit at all.

I tried changing it to POST also, just to see if that was the issue...still doesn't work.

Can anyone point out what I', doing wrong?

EDIT: I've just noticed I'm getting this in Chrome

Refused to set unsafe header "Accept-Encoding" XMLHttpRequest cannot load http://localhost:57631/Api/Products/SubscribeEmailMeWhenAvailable/203/wrfw@wrwq.com?_=1447757623275. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:50217' is therefore not allowed access.

Percy
  • 2,855
  • 2
  • 33
  • 56

1 Answers1

0

This message seems related to gzip compression.

Have you tried this ?

headers: { "Accept-Encoding" : "gzip,deflate,sdch" } 

Result :

$.ajax({
    headers: { "Accept-Encoding" : "gzip,deflate,sdch" } 
    contentType: "application/json; charset=utf-8",
    dataType:'json',
    cache:false,
    type: "GET",
    url: url,
    success: function (data) {
        alert("success");            
    },
    error: function (xhr, ajaxOptions, thrownError){
        alert('Failed to subscribe.');
    }  
});            
FLX
  • 2,626
  • 4
  • 26
  • 57