传递复杂对象时web服务的内部错误

时间:2015-04-21 07:52:12

标签: asp.net ajax json web-services

我被我的网络服务中出现的愚蠢问题所困扰。它抛出异常

({"Message":"An attempt was made to call the method \u0027ManageExpirationRules\u0027 
using a POST request, which is not allowed.","StackTrace":"   
at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)  
at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"});

实体类

namespace Viungo_WebServices.Model
{
    public class ExpirationPolicyType
    {
           public int Id                    {get;set;}
           public string Name               {get;set;}
           public string ShortDescription   {get;set;}
           public DateTime CreatedOn        {get;set;}
           public string CreatedBy          {get;set;}
           public DateTime ModifiedOn       {get;set;}
           public string ModifiedBy         {get;set;}
           public bool IsActive { get; set; }

    }
}

web服务

   [WebMethod]
   [ScriptMethod(UseHttpGet = true)]
   public object ManageExpirationRules(ExpirationPolicyType policyType , int flag)
   {
       var userId = HttpContext.Current.Request["filter"];
       policyType.ModifiedBy= policyType.CreatedBy = userId;
      return new ControllerOffers().ManageExpirationRules(policyType, flag);
   }

这是我的要求机构:

[{policyType :"Id":"","Name":"dsfsdf","ShortDescription":"sdfsdfdf","CreatedBy":"","ModifiedBy":"","IsActive":1}},{flag :1}]

AJAX请求

//Making complex type object
        var policyType = {
            Id: $("#hidRuleId").val(),
            Name: $("#txtRuleName").val(),
            ShortDescription: $("#txtShortDescription").val(),
            CreatedBy: $("#hidUserId").val(),
            ModifiedBy: $("#hidUserId").val(),
            IsActive: 1
        };

        var flag = 1;
        //Convert javascript object to JSON object
        var finalData = "[{policyType :" + JSON.stringify(policyType) + "},{flag :1}]";


        $.ajax({
            url: globalURL + "ManageExpirationRules", //calling Web API controller product
            cache: false,
            type: "POST",
            contentType: "application/json; charset=utf-8",
            data: finalData,
            dataType: "json",
            success: function (data) {
                alert("added");
                $("#hidRuleId").val("");
                $("#txtRuleName").val("");
                $("#txtShortDescription").val("");
                $("#hidUserId").val("");
                $("#lblmsg").html("Rule created successfully");
            }
        }).fail(
        function (xhr, textStatus, err) {
            alert(err);
        });

请帮助我!!!!。

1 个答案:

答案 0 :(得分:0)

问题出在属性[ScriptMethod(UseHttpGet = true)]

如果您添加此属性,则您的网络方法需要GET请求,并且您的错误显示您正在发出POST请求。更改属性或请求方法以使它们匹配。

MSDN:ScriptMethodAttribute.UseHttpGet Property