0

I have problem with ajax query, i try make post query for wcf service and this don't work. Help me please.

enter image description here

My Service, have next code in global.asax

protected void Application_BeginRequest(object sender, EventArgs e)
    {

        EnableCrossDomainAjaxCall();
    }

    private void EnableCrossDomainAjaxCall()
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }

Service contract

[ServiceContract]
public interface IService1
{
    [OperationContract()]
    [WebInvoke(Method = "GET", UriTemplate = "/GetTestData", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    List<Data.Test> GetTestData();

    [OperationContract]
    [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
    void AddTestData(Data.Test value);
}

And svc code

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
    //static data for test
    private static List<Data.Test> _data = new List<Data.Test>() { new Data.Test() { ID = 0, Name = "a" }, new Data.Test() { ID = 1, Name = "B" } };
    public List<Data.Test> GetTestData()
    {
        return _data;
    }

    public void AddTestData(Data.Test value)
    {
        _data.Add(value);
    }
}

and my query

function SendData() {
    var Json = { 'id': $("#id").val(), 'name': $("#name").val() };
    $.ajax({
        type: "Post",
        url: "http://localhost:10468/Service1.svc/AddTestData",
        data: JSON.stringify(Json), 
        contentType: "application/json;charset-uf8", 
        dataType: "json", 
        success: function (msg) {
        },
        error: function (err) {
        }
    });
}

This project on github

I don't knowe whay it's not working. P.S. Sorry for my english.

Voucik
  • 103
  • 3
  • 10
  • try `dataType: "jsonp",` – Grundy Sep 30 '14 at 07:48
  • 1
    possible duplicate of [Cross-Domain AJAX doesn't send X-Requested-With header](http://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header) – Grundy Sep 30 '14 at 07:54

1 Answers1

0

I fix this problem. Need add this parameters (Origin, X-Requested-With, Content-Type, Accept) in (Access-Control-Allow-Headers).

protected void Application_BeginRequest(object sender, EventArgs e)
    {

        EnableCrossDomainAjaxCall();
    }

    private void EnableCrossDomainAjaxCall()
    {
        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Credentials", "true");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
    }
Voucik
  • 103
  • 3
  • 10