Reputation: 1290
I am trying to post to a WCF json service endpoint from angular but have been unsuccessful in my attempts. I have verified the service is working by other means and working for the specified URL.
Using firebug I can see the request being output as such:
NetworkError: 400 Bad Request - http://www.myapi.com/V1/Service.svc/Authenticate?Password=password&UserName=username"
angular code
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);
Request definition
[DataContract]
public class AuthenticateRequest
{
[DataMember]
public String UserName { get; set; }
[DataMember]
public String Password { get; set; }
}
Upvotes: 0
Views: 2093
Reputation: 587
I managed to get this done by stringfy-ing a javascript object.
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;
}
}
}]);
Service Contract
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; }
}
post here helped me a lot. please let me know if you suceed in passing a JSON string
Upvotes: 1