【发布时间】:2015-01-20 20:12:15
【问题描述】:
我在我的 Windows 服务器上发布了 IIS 7.5 上的 WCF 服务
它基本上有两种方法,只是为了烟雾测试。
这里是 ServiceContract:
[ServiceContract]
public interface IFSMServiceWeb
{
[OperationContract]
[WebInvoke(Method="POST", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json)]
string registraUtenteViaMail(string username, string password, string key, string dbVersion);
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped)]
string loginUtenteViaMail(string username, string password, string key, string dbVersion);
}
这里是实现:
public class FSMServiceWeb : IFSMServiceWeb
{
public string registraUtenteViaMail(string username, string password, string key, string dbVersion)
{
return username + password + key + dbVersion;
}
public string loginUtenteViaMail(string username, string password, string key, string dbVersion)
{
return username + password + key + dbVersion;
}
}
我用GET方法调用loginUtenteViaMail方法没问题,但是我不能用POST方法调用方法registraUtenteViaMail方法。
这里是用来测试POST方法的javascript代码
$(document).ready(function(){
$("#send").click(function(){
var username = $("#username").val();
var password = $("#password").val();
var key = $("#key").val();
var dbVersion = $("#dbVersion").val();
var dati = {username : username, password : password, key : key, dbVersion : dbVersion};
$.ajax({
type: 'POST',
url: '/FSMServiceWeb.svc/registraUtenteViaMail',
data: JSON.stringify(dati),
success: function (data) {
console.log(data);},
error: function (data){},
dataType: 'JSON'
});
});
});
我收到此错误(Chrome 控制台):
POST http://xxx.xxx.xxx.xxx/FSMServiceWeb.svc/registraUtenteViaMail 400 (Bad Request) jquery.min.js:4
l.cors.a.crossDomain.send jquery.min.js:4
o.extend.ajax POSTtest.js:12
(anonymous function) jquery.min.js:3
o.event.dispatch jquery.min.js:3
r.handle jquery.min.js:3
我哪里错了?
提前谢谢你
【问题讨论】:
-
能否请您发布 JSON.stringify(dati) 的输出?
-
console.log (JSON.stringify(dati));返回给我这个: {"username":"name","password":"secret","key":"1234","dbVersion":"1.0"}