1

When I try to call wcf service from $.ajax method I am getting following Exceptions.

1. Failed to load resource: the server responded with a status of 405 (Method Not Allowed) 

2. Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource.

AJAX Coding

    $.ajax({
        type: "POST",
        dataType: "json",
        contentType: "application/json",
        data: JSON.stringify(request),
        async: false,
        url: "http://localhost:65201/Empservice.svc/getEmployee",
        crossdomain: true,
        success: function (data) {
            try {
                response = data;
            }
            catch (e) {
                alert(e);
            }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert("Excpetion " + errorThrown + XMLHttpRequest);
        }
    });
Faizan Mubasher
  • 4,427
  • 11
  • 45
  • 81
Arun.DotNet
  • 81
  • 1
  • 8
  • url what you specified is incorrect use url:"your url" – kamesh Feb 27 '14 at 10:21
  • [Have a read here](http://stackoverflow.com/questions/10636611/how-does-access-control-allow-origin-header-work) – StuartLC Feb 27 '14 at 10:24
  • you make a request to cross domain but you defined your dataType as JSON. Change to JSONP and if you trying to access your localhost file mention your ip address inside your url ex:url"yourIpAddress/Empservice.svc/getEmployee" – kamesh Feb 27 '14 at 10:25
  • when use url as string pattern in ajax call after that too we are facing the same response – Arun.DotNet Feb 27 '14 at 10:25
  • we used jsonp instead of json then following method not fire it. success: function (data) { try { response = data; } catch (e) { alert(e); } }, – Arun.DotNet Feb 27 '14 at 10:32
  • because we are getting undefined data in response after execute it – Arun.DotNet Feb 27 '14 at 10:34
  • **It is not possible to make a JSONP POST request**. but i need to use post method to achieve execution – Arun.DotNet Feb 27 '14 at 10:50

1 Answers1

1

To avoid 405 Method not allowed error try to add following coding in wcf serivce Global.asax file.it working for me

 protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {

            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
                           "Accept, Content-Type,customHeader");

            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods",
                          "POST,GET,OPTIONS");

            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age",
                          "172800");

            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials",
                          "true");

            HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers",
                          "customHeader");

            HttpContext.Current.Response.AddHeader("Content-type",
                         "application/json");

            HttpContext.Current.Response.End();
        }
        else
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers",
                           "Accept, Content-Type,customHeader");


            HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers",
                          "customHeader");

            HttpContext.Current.Response.AddHeader("Content-type",
                         "application/json");

        }


    } 
Arun.DotNet
  • 81
  • 1
  • 8