我正尝试从角度发布到WCF json服务端点,但在尝试中未成功。我已经验证该服务正在通过其他方式工作并为指定的URL工作。
使用firebug我可以看到请求输出如下:
NetworkError:400错误请求 - http://www.myapi.com/V1/Service.svc/Authenticate?Password=password&UserName=username"
角度代码
app.service('UserService', function ($http) {
this.GetLoginStatus = function (AuthenticateRequest) {
$http({
url: APIURL + "/Authenticate",
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
params: AuthenticateRequest,
data: {
'Code': 'test data'
}
});
};
});
WCF Iservice
[WebInvoke(RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped)]
[OperationContract]
AuthenticateResponse Authenticate(AuthenticateRequest Request);
请求定义
[DataContract]
public class AuthenticateRequest
{
[DataMember]
public String UserName { get; set; }
[DataMember]
public String Password { get; set; }
}
答案 0 :(得分:1)
我设法通过字符串化javascript对象来完成这项工作。
Service.js
angular.module('myModule').factory('serviceFac', ['$http', '$q',
function (a, b)
{ var taskMergeServiceNameSet = "WebServuice.svc/Tasks/SetTasks";
return {
setTasksMerge: function (taskIds, action) {
var objArray = '';
var obj = {
taskIds: taskIds,
action: action,
userName: "ss"
};
objArray = new Array();
objArray.push(obj);
var deferred = b.defer();
a({
url: taskMergeServiceNameSet,
method: 'POST',
headers: { 'Content-Type':'application/json; charset=utf-8'},
data: JSON.stringify(objArray)
}
).sucess(function (data) { deferred.resolve(data) })
.error(function (msg, code) { deferred.reject(msg) });
return deferred.promise;
}
}
}]);
服务合同
ServiceContract Interface
[ServiceContract]
public interface ITasks
{
[OperationContract]
[WebInvoke(Method = "POST",RequestFormat=WebMessageFormat.Json ,
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Tasks/SetTasksForMerge")]
string CreateNewTaks(valObj[] values);
}
[DataContract]
public class valObj
{
[DataMember]
public string taskIds { get; set; }
[DataMember]
public string action { get; set; }
[DataMember]
public string userName { get; set; }
}
帖子here给了我很多帮助。如果您通过JSON字符串
,请告诉我