【发布时间】:2012-08-24 14:00:11
【问题描述】:
我用 wcf 创建了一个应用程序,然后我在使用 Type Post 的操作时遇到问题,他 retourn Method not allowed ,这是我的操作
[WebInvoke(Method = "POST", UriTemplate = "newContact", ResponseFormat = WebMessageFormat.Json)]
[AuthorizedMethod]
bool CreateContact(string jsonstring);
在这个页面中,我将类型字符串转换为 json
public bool CreateContact(string jsonstring)
{
Dictionary<string, string> values = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonstring);
log();
return dispatcher.CreateContact(values);
这是我的服务
<system.serviceModel>
<services>
<service behaviorConfiguration="Default" name="Mobility.SolutionService.AppService">
<host>
<baseAddresses>
<add baseAddress="https://localhost:443/B2M/"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" bindingConfiguration="crossDomain"
contract="Mobility.SolutionService.IAppService" behaviorConfiguration="Web"/>
</service>
</services>
<bindings>
<webHttpBinding>
<binding name="crossDomain">
<security mode="Transport">
<transport clientCredentialType="Basic" >
</transport>
</security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceDebug includeExceptionDetailInFaults="true"/>
<serviceMetadata httpsGetEnabled="true" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="Mobility.SolutionService.CustomUserNamePasswordValidator, Mobility.SolutionService"/>
<serviceCertificate findValue="CN=AuthorityName"/>
</serviceCredentials>
<serviceAuthorization principalPermissionMode="Custom">
<authorizationPolicies>
<add policyType="Mobility.SolutionService.CustomAuthorizationPolicy, Mobility.SolutionService"/>
</authorizationPolicies>
</serviceAuthorization>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="Web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
然后我用js发送数据
function CreateClient(button){
var strN = document.getElementById('nom').value;
var str1 = document.getElementById('Phone').value;
var str2 = document.getElementById('Address').value;
var str10 = document.getElementById('Credit_Limit_LCY').value;
var dict = [];
dict.push(
{
key:"nom",
value: strN
},
{
key:"Phone",
value:str1
},
{
key: "Address",
value:str2
},
{
key:"Credit_Limit_LCY",
value:str10
}
);
var Mydata=JSON.stringify(dict);
$.mobile.showPageLoadingMsg();
var geturl;
geturl =$.ajax({
url:"https://10.0.2.2:443/B2M/newContact/",
data:Mydata,
contentType:"application/json; charset=utf-8",
dataType:"json",
timeout:10000000,
cache:false,
type:'POST',
beforeSend : function(req) {
req.setRequestHeader('Authorization',
make_base_auth (val1,val2));
},
error:function(XMLHttpRequest,textStatus, errorThrown) {
$.mobile.hidePageLoadingMsg();
alert("Error status :"+textStatus);
alert("Error type :"+errorThrown);
alert("Error message :"+XMLHttpRequest.responseXML);
},
success:function(data) {
alert("success");
$.mobile.hidePageLoadingMsg();
$.mobile.changePage("contact.html", "fade");
}
});
}
你能帮帮我吗 谢谢
【问题讨论】:
-
您能澄清一下您的问题吗?感谢您列出您的代码,但您在哪里看到错误?您说“我对 Type Post 的操作有问题,他返回了不允许的方法”,但我不确定您在哪里看到“不允许的方法”错误 - 在访问服务/应用程序的 Web 浏览器中?跨度>
-
是的服务只是打印控制台中不允许的方法
-
你能区分出哪个方法被引用了吗?不幸的是,听起来问题出在你的 WCF 上,这不是我的专业领域(虽然我做了很多 jQuery)。您的服务是否需要身份验证/授权?如果是这样,也许您应该尝试暂时关闭它以进行调试。