【发布时间】:2011-07-29 22:53:56
【问题描述】:
我正在关注各种在线示例,但均未成功。我只是想创建一个将值传递给 Web 服务调用的初始示例。
我做错了什么?
我可以用 HttpHandlers 轻松做到这一点……这么简单的事情不应该这么难吗?
更新:
它失败的原因是缺少“contentType”属性。这在following answer 中有概述。
这是我不断遇到的错误:
System.InvalidOperationException: Missing parameter: key.\r\n at System.Web.Services.Protocols.ValueCollectionParameterReader.Read(NameValueCollection collection)\r\n at System.Web.Services.Protocols.HtmlFormParameterReader.Read(HttpRequest request)\r\n at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()\r\n at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
这是 HTML:
<script type="text/javascript">
var url = '<%=ResolveUrl("~/Services/ProjectDialog.asmx/TryThis")%>';
function callWebService() {
jQuery.ajax({
cache: false,
type: 'POST',
complete: onComplete,
data: '{ "key": 42 }',
dataType: 'application/json; charset=utf-8',
error: onError,
success: onSuccess,
url: url
});
}
function onComplete(status, xmlHttpRequest) {
var stop = "";
}
function onError(xmlHttpRequest, status, error) {
var stop = "";
}
function onSuccess(data, status, xmlHttpRequest) {
var stop = "";
}
jQuery(document).ready(function() {
});
</script>
<input type="button" value="Run Web Service" onclick="callWebService();" />
这是网络服务:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Serialization;
namespace My.Services
{
/// <summary>
/// Summary description for ProjectDialog
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class ProjectDialog : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string TryThis(Int32 key)
{
return key.ToString();
}
}
}
【问题讨论】:
-
感谢大家的帮助...我会通过评论让人们知道什么有效,什么无效。
标签: jquery web-services