在传递两个参数时,我无法从 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
O,我还尝试关闭代码调试来跟踪错误是什么,但由于某种原因我没有看到任何异常。
最佳答案
您的服务采用 int
值作为参数,但您向其发送字符串。如果 customerId
和 departmentId
是数字,您需要删除它们周围的引号,以便将它们解释为数字。
'{"customerId": ' + customerId + ', "departmentId": ' + departmentId + '}'
关于Jquery Ajax WCF 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15145318/