I built a RESTful service with ServiceStack which sends data to a database. I\'ve tested it locally and it works great. When I deploy it to a server and run the same code,
I had the same problem with you, in my previous question
You can read very helpful answers of #mythz here and here .
The code I use in the AppHost
using System.Web;
using ServiceStack.WebHost.Endpoints.Extensions; // for httpExtensions methods
// => after v.3.9.60, =>using ServiceStack;
and
public override void Configure(Container container)
{
SetConfig(new ServiceStack.WebHost.Endpoints.EndpointHostConfig
{
DefaultContentType = ContentType.Json,
ReturnsInnerException = true,
WsdlServiceNamespace = "http://www.servicestack.net/types"
});
Plugins.Add(new CorsFeature());
this.RequestFilters.Add((httpReq, httpRes, requestDto) =>
{
//Handles Request and closes Responses after emitting global HTTP Headers
if (httpReq.HttpMethod == "OPTIONS")
httpRes.EndServiceStackRequest(); //httpExtensions method
// =>after v.3.9.60, => httpRes.EndRequestWithNoContent();
});
Routes
.Add<TestRequest>("/TestAPI/Reservation", "POST, OPTIONS"); // OPTIONS is mandatory for CORS
}
and in JavaScript like you
jQuery.support.cors = true;
function TestRequestCall() {
var TestRequest = new Object();
TestRequest.Id = 11111;
TestRequest.City = "New York";
$.ajax({
type: 'Post',
contentType: 'application/json',
url: serverIP +'/TestAPI/Reservation',
data: JSON.stringify( TestRequest ),
dataType: "json",
success: function (TestResponse, status, xhr) {
if(TestResponse.Accepted) doSomething();
},
error: function (xhr, err) {
alert(err);
}
});
}