I have problem with ajax query, i try make post query for wcf service and this don't work. Help me please.
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) {
}
});
}
I don't knowe whay it's not working. P.S. Sorry for my english.