【问题标题】:Jquery Ajax WCF ParametersJquery Ajax WCF 参数
【发布时间】:2013-02-28 20:46:06
【问题描述】:

在传递两个参数时,我无法从 jquery 获得 WCF 调用。如果我稍微更改代码以仅通过一件事就可以了。

Javascript:

 $.ajax({
    type: "POST", //GET or POST or PUT or DELETE verb
    url: "/Services/JobNumberService.svc/GetActiveJobNumberByCustomerOrJointBilling", // Location of the service
    data: '{"customerId": "' + customerId + '", "departmentId": "' + departmentId + '"}', //Data sent to server
    contentType: "application/json; charset=utf-8", // content type sent to server
    dataType: "json", //Expected data format from server
    processdata: true, //True or False
    success: function (msg) {//On Successfull service call
        fillDropDownFromLookupList(msg, jobNumberDropDownId);

        prependItemToDropDown('', 'NONE', jobNumberDropDownId); //Add blank item to top of list
    },
    error: ServiceFailed// When Service call fails
});

服务签名:

public LookupList GetActiveJobNumberByCustomerOrJointBilling(int customerId, int departmentId)

这与我格式化传入的 json 的方式有关。根据 JSONLint 它是有效的,但可能不是 .net 所期望的。

感谢您的想法。

编辑

这就是我在回复中得到的回复

HTTP/1.1 500 Internal Server Error
Server: ASP.NET Development Server/11.0.0.0
Date: Thu, 28 Feb 2013 20:30:17 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Length: 0
Connection: Close

哦,我还尝试关闭我的代码调试以跟踪错误是什么,但由于某种原因我没有看到任何异常。

【问题讨论】:

  • 尝试逐步执行服务器端执行并查看 500 错误是什么 - 或在配置中打开详细错误消息,以便响应将包含堆栈跟踪。这是相当模棱两可的。
  • 嘿,托尼,您想到的详细错误设置在哪里?我在 serviceBehaviors 配置部分有 includeExceptionDetailInFaults="true"。
  • 您应该能够通过编辑web.config 来打开它们,如下所示:<system.web><customErrors mode="On" /></system.web> - 如果这不起作用,请告诉我,我会帮助您进一步挖掘。
  • 好主意,我已经很遗憾地打开了它
  • 嘎——这就是我快速打字的结果。 customErrors 需要设置为 Off 才能获得详细信息。

标签: jquery asp.net ajax wcf


【解决方案1】:

您的服务将int 值作为参数,但您发送的是字符串。如果customerIddepartmentId 是数字,则需要删除它们周围的引号,以便将它们解释为数字。

'{"customerId": ' + customerId + ', "departmentId": ' + departmentId + '}'

【讨论】:

  • 没想到,我一直用JSON.stringify生成正确的JSON。
  • 我不认为这是问题所在。 #1,当我只传递其中一个参数时,服务器就被调用了。 #2 我试过了,但没有用。让我用我看到的错误来更新问题
  • @AndrewWalters 您的服务是否需要 json?将值作为数字发送时是否收到 500 错误?
  • 是的,如果您按照您的建议传递数据,则会出现同样的错误。我确定我在做一些愚蠢的事情,只是需要一些额外的眼睛才能弄清楚
【解决方案2】:

根据您的 cmets,我相信您的问题可能是您尝试将多个参数绑定到您的端点。我不是 WCF 专家,但我知道这在 WebAPI 中不起作用。

一般来说,解决方法是为您的绑定创建一个模型以解析为:

public class CustomerModel { 
    public int customerId; 
    public int departmentId; 
}

然后将您的调用的预期参数设为:

public LookupList GetActiveJobNumberByCustomerOrJointBilling(CustomerModel model)

现在您只需要从您提交的 JSON 对象周围删除引号,您的 AJAX 应该可以正常工作,因为以下 JSON 对象将正确反序列化为您的新 CustomerModel 对象:

{ "customerId": 1, "departmentId": 1 }

如果保留引号,您可能会得到错误的结果 - 因为解析器会将其接收的数据解释为字符串,而不是 JSON 对象。

【讨论】:

  • 我会从你那里得到它,我的答案是正确的,这有帮助。 :) 很高兴我能提供帮助!
猜你喜欢
  • 1970-01-01
  • 2011-12-30
  • 1970-01-01
  • 1970-01-01
  • 2016-10-22
  • 2015-12-31
  • 2014-08-15
  • 2011-08-24
  • 2010-10-13
相关资源
最近更新 更多