【问题标题】:Angular $http post to WCF service null argumentAngular $http 发布到 WCF 服务空参数
【发布时间】:2014-04-18 04:23:36
【问题描述】:

我有简单的 Angular js 应用程序。我有一个简单的身份验证方法。在 Internet Explorer 中执行该方法时,WCF 方法会接收带有数据的参数。当从 firefox 执行 ame 代码时,服务器上的参数为 null。

$http({
    url: Url,
    method: "POST",
    headers: {
        'Content-Type': 'application/json; charset=utf-8'
    },
    data: AuthenticateRequest
});

WCF 方法

    [WebInvoke(RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json,
    Method = "*",
    BodyStyle = WebMessageBodyStyle.Bare)]
    [OperationContract]
    AuthenticateResponse Authenticate(AuthenticateRequest AuthenticateRequest);

Fiddler IE 信息

POST http://www.api.com:56586/V1/Service.svc/json/Authenticate HTTP/1.1
X-Requested-With: XMLHttpRequest
Accept: application/json, text/plain, */*
Content-Type: application/json; charset=utf-8
Referer: http://www.api.com:54567/
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; Touch; rv:11.0) like Gecko
Host: localhost:56586
Content-Length: 45
DNT: 1
Connection: Keep-Alive
Pragma: no-cache

{"UserName":"username","Password":"password"}

Firefox Fiddler 信息

OPTIONS http://www.api.com:56586/V1/Service.svc/json/Authenticate HTTP/1.1
Host: www.api.com:56586
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Origin: http://www.api.com:54567
Access-Control-Request-Method: POST
Access-Control-Request-Headers: content-type,x-requested-with
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache

【问题讨论】:

  • FF 发送的是OPTIONSrequest,而不是POST。因此你没有得到有效载荷。您的 Web 应用程序和 Web 服务是否托管在同一主机/端口上?您是否在浏览器 javascript 控制台中获得任何信息?

标签: c# json wcf angularjs firefox


【解决方案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 字符串,请告诉我

【讨论】:

    猜你喜欢
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-20
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多