0

I am trying to call WCF Services using AJAX, below is my code:

$.ajax({
    url: "http://localhost/TestingServices/Service1.svc/GetData"
    data: "{'value:1}",
    type: "POST",
    dataType: "json",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        alert(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) {
        alert(textStatus);
    }
});

But after execution it gives me following error:

XMLHttpRequest cannot load 
  http://localhost/TestingServices/Service1.svc/GetData.Response to 
  preflight request doesn't pass access control check: 
  No 'Access-Control-Allow-Origin' header is present on 
  the requested resource. Origin 'null' is therefore not 
  allowed access. The response had HTTP status code 404.

Can anyone help me how to resolve this ?

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
tiya
  • 111
  • 1
  • 8
  • remove contentType: "application/json; charset=utf-8" and then try,,, – Dhara Parmar May 13 '16 at 06:01
  • kindly refer http://stackoverflow.com/questions/33820142/getting-request-doesnt-pass-access-control-check-no-access-control-allow-orig – RRR May 13 '16 at 06:04
  • Now,i get dis error: POST http://localhost/Wcf/service1.svc/GetData 415 (Cannot process the message because the content type 'application/x-www-form-urlencoded; charset=UTF-8' was not the expected type 'text/xml; charset=utf-8'.) – tiya May 13 '16 at 06:04

2 Answers2

0

Try 2 Add HttpProtocols in web.config of your service project

    <system.webServer>
    <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
        <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
      </customHeaders>
    </httpProtocol>
</system.webServer>
0

please try this one....

$.ajax({
type: "GET",
url: "http://localhost/TestingServices/Service1.svc/GetData",
dataType: "jsonp",
success: readData(data),
error: function (xhr, ajaxOptions, thrownError) {
  alert(xhr.status);
  alert(thrownError);
 }
})
Banwari Yadav
  • 506
  • 1
  • 3
  • 18