hello my problem is that i use microsoft visual studio for creating wcf webservice and when i run on microsoft visual studio everything is fine and works. But i have to connect to webservice outside so when i do that its not connect. so first its give me crossDomain error so i change webconfig and i get value from outside html file but i cant post. I gave this errro :
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)
my english is not good so i will add my code and source file you can see yourself.
first my javascript :
<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>
my service content :
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="Rami@Rami.com"},
new User() { Name="Bill", Email="Bill@Bill.com"},
new User() { Name="Mark", Email="Mark@Mark.com"},
};
// 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; }
}
}
and i add webconfig this for crossdomain
<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>
i give you my solution file its working , also i give you outside html file get is working post not.
i used jsonp and i change binding in web.config. It is working now.
You can try like that :
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
<handlers>
<remove name="OPTIONSVerbHandler" />
<add name="OPTIONSVerbHandler" path="*" verb="OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" resourceType="Unspecified" requireAccess="None" preCondition="bitness32" />
</handlers>
</system.webServer>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.