I have WCF service in web application and have Ajax client in another web application. In terms of cross-domain issue, I have no problem with GET ajax call, but I have a problem with POST ajax call. I am not sure this is from cross-domain issue though. Anyway, when GET ajax invokes WCF service successfully, but it does not in case of POST ajax invocation.
WCF Service
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "UserService/AddUser", BodyStyle = WebMessageBodyStyle.WrappedRequest)]
public User AddUser(User input)
{
var user = input;
// Do something business logic
return user;
}
Global.asax in web application that WCF Service resides
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();
}
Ajax code
<script type="text/javascript">
$(document).ready(function () {
$("#submit").click(function () {
$.ajax({
cache: false,
type: "POST",
async: false,
url: "http://localhost:2000/UserService/AddUser",
data: { "LoginId" : $("#LoginId").val(), "Name" : $("#Name").val() },
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function (userViewModel) {
var user = userViewModel;
alert(user);
}
});
});
});
</script>
I debugged it with developers tool in Chrome browser and I got following message
Request URL:http://localhost:2000/UserService/AddUser
Request Method:POST
Status Code:400 Bad Request
Request Headersview source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Charset:windows-949,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:ko-KR,ko;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:18
Content-Type:application/json; charset=UTF-8
Host:localhost:2000
Origin:http://localhost:3000
Referer:http://localhost:3000/views/useradd.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7
Request Payload
LoginId=11&Name=22
Response Headersview source
Access-Control-Allow-Origin:*
Cache-Control:no-cache, no-store
Connection:Close
Content-Length:1760
Content-Type:text/html
Date:Tue, 14 Feb 2012 05:56:42 GMT
Expires:-1
Pragma:no-cache
Server:ASP.NET Development Server/10.0.0.0
X-AspNet-Version:4.0.30319
As you see it is 400 Bad Request, this is why I doubted that the problem is related with cross-domain issue. But I am not sure.
Can you guess the reason of this problem? Thanks in advance.