I am doing a cross-domain json post utilizing asp.net web api. On my service / server side I have the following method that response to the POST
request:
// POST api/<controller>
public HttpResponseMessage Post(Customer newCustomer)
{
DataClassesDataContext db = new DataClassesDataContext();
var response = Request.CreateResponse<Customer>(HttpStatusCode.Created, newCustomer);
db.Customers.InsertOnSubmit(newCustomer);
db.SubmitChanges();
string uri = Url.Link("DefaultApi", new { id = newCustomer.Id });
response.Headers.Location = new Uri(uri);
return response;
}
And on the client side, which run on a different port number (i.e. cross-domain), I am using the following jQuery code:
<body>
<input type="button" value="click me" onclick="hello()" />
<script type="text/javascript">
function hello() {
//SEE: http://stackoverflow.com/questions/5241088/jquery-call-to-webservice-returns-no-transport-error
$.support.cors = true;
//var jsonObjects = [{ Name: "Name1", CellphoneNum: 1234657911 }];
$.ajax({
url: "http://localhost:26037/api/customer",
type: "POST",
crossDomain: true,
data: { Name: "Name321", CellphoneNum: 1234657922 },
dataType: "json",
success: function (result) {
alert("Success");
},
error: function (xhr, ajaxOptions, thrownError) {
alert('Thrown Error: ' + thrownError);
alert('xhr status: ' + xhr.status);
}
});
}
</script>
</body>
My issue is that, from the client side, on IE the success alert message will be displayed and the data will be inserted to the database using the LINQ code in my POST method. However, using Chrome or Firefox the data will also get inserted to the database and LINQ code will be executed successfully but a message box with an empty string error and a 0 xhr status will be displayed.
Why is it only reporting an error on Firefox and Chrome while the data get posted and successfully processed in the server?