Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I create a RESTFull WCF Service with POST Method which is called by Ajax from ASP.NET or PhoneGap
Posted
Comments
DamithSL 18-Aug-14 4:36am    
Ameer Vikram 18-Aug-14 5:24am    
yes I am and I have created a Test service. but the POST from Ajax is not Working. GET Method is working fine. I have checked the issue and got some hints like, "cross-domain" issue. But I can't resolved that. Pleas help me. Thanks in Advance
DamithSL 18-Aug-14 5:30am    
can you update the question with your code?
Ameer Vikram 18-Aug-14 5:44am    
This is my code
----------------------------

[ServiceContract]
public interface IUserSevices
{
[OperationContract]
[WebInvoke(UriTemplate = "/UserChangePassword", Method = "POST", RequestFormat = WebMessageFormat.Json)]
string ChangePassword(Users user);
}



public string ChangePassword(Users user)
{
return user.NewPassword;
}




<script>

function ImporterChangePassword() {
var impData = {
"ExporterCDId": "3",
"ImporterId": "5",
"CompanyCode": "COMCODE999",
"OldPassword":"imp2",
"NewPassword": "imp1"
};
$.ajax({
cache: false,
async: true,
type: "POST",
url: "http://localhost:35153/TestService.svc/Users/UserChangePassword",
data: JSON.stringify(impData),
contentType: "application/json; charset=utf-8",
dataType: "json",
processData: true,
success: function (data, status, jqXHR) {
alert("success..." + data);
},
statusCode: {
400: function (jxhr, textStatus, errorThrown) {
alert(textStatus);
},
401: function () {
alert("401");
}
},
error: function (xhr) {
alert(xhr.responseText);
}
});
}

</script>
j snooze 18-Aug-14 17:53pm    
what does your web.config look like. Thats usually the biggest part of setting a web service up in wcf. Also I think for cross domain ajax calls you have to use jsonp vs json. I was banging my head against a wall until I found that also enabled cross domain script access in by web.config webHttpBinding bindings setup. Binding attribute is crossDomainScriptAccessEnabled="true"

1 solution

Not sure if your cross domain issue got resolved, but once I had a bad issue with cross domain in one of my project and let me share with you. There were 2 projects basically, one was a ASP.NET Umbraco CMS front end and another a Web API project on the back end. Both the projects were hosted as different projects and with different domains. and the Umbraco front end used to do $.ajax GET/POST to the web api and we started facing cross domain issue. I tried to do everything like enabling cross domain and other stuff and was close to achieving it but couldn't get it to working on all browsers.
As i had a strict deadline we designed the communication between the front end and the back end websites in a different way. So what we did was instead of making $.ajax calls from the front end website to the back end web api, we made $.ajax calls to an endpoint in the front end website(yes a design change). And from the front end C# code, we made WebClient calls to the back end web api which is perfectly valid as server to server communication allows cross domain. And after receving the response from the backend web api, we just returned the response to our $.ajax success method and thats how we solved the cross domain problem. Not sure if its the best solutions but we did it and we were successful.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900