M.Mohammadi
M.Mohammadi

Reputation: 1567

How to solve warning CORS request failed when call wcf service?

I publish my service on IIS 7.5, but I couldn't call my service in Ajax ,it should be noted that i setup all of setting for cross on my service and i when call service with Ajax received two warning in my console:

1-Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://84.241.41.5:8000/Sale.svc/GetDataReportSaleOfMonth. (Reason: CORS header 'Access-Control-Allow-Origin' does not match '*, *').

2-Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://84.241.41.5:8000/Sale.svc/GetDataReportSaleOfMonth. (Reason: CORS request failed).

URL of my service activate in address

My Ajax code is :

$.ajax({ 
 type: "Get",
 url: "http://84.241.41.5:8000/Sale.svc/GetDataReportSaleOfMonth", 
 contentType: "application/json;charset-uf8", // content type sent to server
 dataType: "json", //Expected data format from server
 crossDomain: true,
 success: function(response) {//On Successfull service call
            ServiceSucceeded(msg);
        },
 error: function (response, errorText) {
        ServiceFailed(response);
        }// When Service call fails
    });
});

function ServiceFailed(result) {
        alert('Service call failed: ' + result.status + '' + result.statusText);

    }

 function ServiceSucceeded(result) {
         alert("ok");

        }

Please help me that what is reason of error my service in Ajax.

Upvotes: 0

Views: 1514

Answers (1)

haxim
haxim

Reputation: 196

Running your code in Chrome produces the following error:

XMLHttpRequest cannot load http://84.241.41.5:8000/Sale.svc/GetDataReportSaleOfMonth. The 'Access-Control-Allow-Origin' header contains multiple values '*, *', but only one is allowed. Origin 'null' is therefore not allowed access.

When I make a request to the API you posted, I'm presented with the following headers:

Cache-Control: private
Content-Length: 45
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
Access-Control-Allow-Origin: *
Access-Control-Request-Method: POST,GET,PUT,DELETE,OPTIONS
Access-Control-Allow-Headers: accept,Content-Type
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,PUT,POST,DELETE,OPTIONS
Access-Control-Allow-Headers: accept,Content-Type
Date: Wed, 24 Jun 2015 03:56:11 GMT

Notice that the Access-Control-Allow-Origin: * header appears twice. Look at your server implementation, and try removing one of them.

Upvotes: 1

Related Questions