【问题标题】:Internal error with webservice while passing complex object传递复杂对象时 Web 服务出现内部错误
【发布时间】:2015-04-21 07:52:11
【问题描述】:

我遇到了网络服务中出现的愚蠢问题。它抛出异常

({"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; }

    }
}

网络服务

   [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);
        });

请帮帮我!!!!.

【问题讨论】:

    标签: asp.net ajax json web-services


    【解决方案1】:

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

    如果添加此属性,您的 Web 方法需要 GET 请求,而您的错误表明您正在发出 POST 请求。更改属性或请求方法以使其匹配。

    MSDN:ScriptMethodAttribute.UseHttpGet Property

    【讨论】:

      猜你喜欢
      • 2014-10-28
      • 1970-01-01
      • 2014-07-06
      • 2013-03-16
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      相关资源
      最近更新 更多