Hey there, I'm facing an issue with using Microsoft Visual Studio to create a WCF web service. Everything runs smoothly within Visual Studio, but when I try to connect to the service from outside, it fails to establish a connection. At first, I encountered a cross-domain error which I resolved by modifying the webconfig file and sourcing values from an external HTML file. However, I am now unable to perform a POST request and receiving this specific error:
GET http://localhost:3281/UserService.svc/SetUser?callback=jQuery11020891759618…20%22usertype%22:%20%22%22,%20%22email%22%20:%20%22%22%20}&_=1386343306675 405 (Method Not Allowed)
Due to my limited English proficiency, I have included my code and source files for your review.
Here is my JavaScript snippet:
<script>
function btnSubmit() {
$.support.corps = true;
$.ajax({
crossDomain: true,
cache: false,
async: false,
type: "POST",
url: "http://localhost:3281/UserService.svc/SetUser",
data: '{ "usertype": "' + $("#txtuserName").val() + '", "email" : "' + $("#txtuserEmail").val() + '" }',
contentType: "application/json;charset=utf-8",
dataType: "jsonp",
success: function (r) { alert("Successfully Registered!!!"); },
error: function (e) { alert(e.statusText); }
});
}
function btnRetrieve() {
$.support.corps = true;
$.ajax({
crossDomain: true,
cache: false,
async: false,
type: "GET",
url: "http://localhost:3281/UserService.svc/GetUser",
data: { name: $("#find").val() },
contentType: "application/json;charset=utf-8",
dataType: "jsonp",
success: function (r) {
if (r != null) $("#DisplayResult").html(r.Email);
else
$("#DisplayResult").html("Bilgi yok");
},
error: function (e) { alert(e.statusText); }
});
}
</script>
And here's the content of my service:
namespace JQueryCallToWcf
{
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UserService
{
// Create a List of User type and add temporary data. This List will be used a Fake Repository for Data Tranasaction. In real world we can replace this with EntityFramework or Linq2Sql Data Model for actual data transactions.
public static List<User> lstUsers = new List<User>()
{
new User() { Name="Rami", Email="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82d0e3efebc2d0e3efebace1edef">[email protected]</a>"},
new User() { Name="Bill", Email="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f1d3633331f1d363333713c3032">[email protected]</a>"},
new User() { Name="Mark", Email="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f3be928198b3be92...
};
// We have two service methods - SetUser and GetUser, which sets and gets user from fake repository.
[OperationContract]
[WebInvoke(
Method = "POST",
BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
public void SetUser(string usertype, string email)
{
lstUsers.Add(new User() { Name = usertype, Email = email });
}
[OperationContract]
[WebGet(
ResponseFormat = WebMessageFormat.Json)]
public User GetUser(string name)
{
User op = lstUsers.Where(p => p.Name == name).FirstOrDefault();
return op;
}
}
// This is the User Class, holding properties for Name and email.
[DataContract]
public class User
{
[DataMember]
public string Name { get; set; }
[DataMember]
public string Email { get; set; }
}
}
I've also added this to my webconfig file for handling cross-domain requests:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingWithJsonp" crossDomainScriptAccessEnabled="true" />
</webHttpBinding>
</bindings>
<services>
<service name="JQueryCallToWcf.UserService">
<endpoint address="" binding="webHttpBinding"
bindingConfiguration="webHttpBindingWithJsonp"
contract="JQueryCallToWcf.UserService"
behaviorConfiguration="webHttpBehavior"/>
</service>
</services>
</system.serviceModel>
For a full look at my working solution file and the issue with posting from an external HTML file, please visit this link: