Hi,
I am trying to access WCF service using post request/response using Ajax Json request in FireFox,Safari,Chrome, its working currently in internet explorer only.
My Web.config
My ServiceContract
[ServiceContract(Namespace="")]
public interface IUtilityWrapperService
{
[OperationContract]
[WebInvoke(UriTemplate = "/Utility/NewLookup", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat=WebMessageFormat.Json)]
TransactionResponse FillLookup(LookupEntity entity);
}
My APIMessageInspector
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", "X-PINGOTHER");
if (WebOperationContext.Current.IncomingRequest.Method != "OPTIONS")
{
string actionName = OperationContext.Current.IncomingMessageProperties["HttpOperationName"] as string;
if (string.IsNullOrEmpty(actionName))
throw new WebFaultException("Invalid Operation", System.Net.HttpStatusCode.NotFound);
}
else
{
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE,OPTIONS");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", " X-PINGOTHER, Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With, Accept");
// WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
}
return null;
}
public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
//WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Origin", "*");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE,OPTIONS");
WebOperationContext.Current.OutgoingResponse.Headers.Add("Access-Control-Allow-Headers", " X-PINGOTHER, Cache-Control, Pragma, Origin, Authorization, Content-Type, X-Requested-With, Accept");
// WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
Console.WriteLine("Message Response: {0}", reply.ToString());
}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
My Js Call
function SendDataCall() {
var url = "Utility/NewLookup";
var dataColl = { "LookupEKey": "Trolley", "LookupEValue": "Wooden" };
var resultOut = new ServiceWCFJSON(url, ServiceResult, "POST", "json", dataColl);
ServiceResult(resultOut);
}
function ServiceWCFJSON(urlMethod, svcOutput, svcType, svcDataType, dataColl) {
varType = svcType;
varUrl = "http://localhost:1280/UtilityWrapperService.svc/" + urlMethod + "";
//varData = {"fetchType" : "Trolley"};
varData = dataColl;
varContentType = "application/json; charset=utf-8";
varDataType = svcDataType;
varProcessData = true;
return CallService(svcOutput);
}
function CallService(ServiceSucceeded) {
$.ajax({
type: varType, //GET or POST or PUT or DELETE verb
url: varUrl, // Location of the service
data: JSON.stringify(varData), //Data sent to server
//data:varData,
contentType: varContentType, // content type sent to server
dataType: varDataType, //Expected data format from server
processdata: varProcessData, //True or False
crossDomain: true,
async: true,
success: ServiceSucceeded,//On Successfull service call
error: ServiceFailed// When Service call fails
});
}
What I have tried:
If i use HttpRequester addon tool on firefox its working fine. Its CORS issue thats showing in debugger console in firefox, if i send "contentType=
Hide Copy Code
application/x-www-form-urlencoded
its able to hit the APIMessageInspector with POST as HttpOperation, but no interaction with ContractMethod otherwise its just OPTIONS. I am stuck, i want to create WCF service in separate solution and html separate,so that I can consume the html with phonegap build + web using the same service.