0

I'm sorry to ask this question again, but I didn't find a fitting answer to my problem.

I try to build the following: I want to connect to a WCF-Webservice with a standard Webpage. Both website and web service are hosted in an IIS on the same machine.

I enabled cross-domain to the web.config:

<webHttpBinding>
    <binding name="webHttpBindingWithJSONP" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>

And my AJAX request:

$.ajax({
    type: "POST",
    url: config.endpoints.db.production + "AddData/",
    data: JSON.stringify(data),
    contentType: "application/json",
    dataType: "json",
    crossDomain: true,
    //  processData: true,
    success: function(data, status, jqXHR) {
        //  alert("success..." + data);
        // loadingVisible(false);
        //  loadingFinished = true;


    },
    error: function(xhr) {
        alert("failure..." + xhr.responseText);
        //    loadingFinished = true;
        //    loadingVisible(false);

    }
});

After firing the request I got an "Access denied"-Error in the visual studio from jquery. After searching in the web I found out that this is a very common cross-domain problem, but I didn't find a solution. I tried to set "crossDomain: true" in the ajax-request, tried to use jsonp (which worked for my GET-requests) but nothing helped.

Is there a proper way to solve this? I read that this problem might be solved with an ajax authentication. Is that correct and how can I achieve that?

GrandMasterFlush
  • 6,269
  • 19
  • 81
  • 104

1 Answers1

0

To solve the problem do the following

Create a Global.asax and add following to enable Ajax cross-domain POST

 public void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST,OPTIONS");

            if ((HttpContext.Current.Request.HttpMethod == "OPTIONS"))
            {

                HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
                HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
                HttpContext.Current.Response.End();
            }
        }
    }

At your service set

[AspNetCompatibilityRequirements(RequirementsMode =
    AspNetCompatibilityRequirementsMode.Allowed)]
    public class YourserviceName : YourserviceNameInterface