I am trying to call a WCF service Method using Http POST Request from another Project , it allows GET request but when I try for POST request it is showing "Method not allowed".
When i tried it shows this error in my Console:
Access to XMLHttpRequest at 'http://localhost:7280/api/values/24' from origin 'http://localhost:6538' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
Web API: Method:
[WebInvoke(UriTemplate = "/values", Method = "POST", ResponseFormat = WebMessageFormat.Json), CorsEnabled]
void AddValue(string value);
Method Implementation
public void AddValue(string value)
{
string id = nextId.ToString();
nextId++;
allValues.Add(id, value);
string location = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.RequestUri.ToString() + "/" + id;
WebOperationContext.Current.OutgoingResponse.SetStatusAsCreated(new Uri(location));
}
Client Application:
$("#post").click(function () {
var data = "\"" + $("#value").val() + "\"";
$.ajax({
url: valuesAddress,
type: "POST",
contentType: "application/json",
data: data,
success: function (result) {
$("#result").text(result);
},
error: function (jqXHR, textStatus, errorThrown) {
$("#result").text(textStatus);
}
});
});
I followed https://code.msdn.microsoft.com/Implementing-CORS-support-c1f9cd4b/view/SourceCode#content I created web API and client Application as different projects. Please help to solve this issue.