2014-04-17 48 views
0

我想從角度發佈到WCF json服務端點,但在嘗試中失敗。我已經驗證該服務正在通過其他方式工作,併爲指定的URL工作。以角度發佈到WCF json端點

使用螢火蟲我可以看到該請求被輸出爲例如:

NetworkError: 400 Bad Request - 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

你在哪裏設置/定義你的服務的基本認證?你確定auth字符串是好的?你使用郵遞員在鉻? – aet

+0

我更新了我的代碼並刪除了該行。我確實使用郵遞員。 – CodeMilian

+0

我對WCF並不是很熟悉,但不是用於添加XML元素的wrapped()樣式?如果意圖是讓api只說json,也許你不想要那個?另外,我不禁想到你可能想要在數據屬性中發送正文中的內容,而不是查詢參數(因爲服務期望POST)。 – aet

回答

1

我設法通過stringfy-ING 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字符串