将值传递给jQuery';什么是web服务调用?
我正在跟踪各种在线示例,但没有任何成功。我只是尝试创建一个将值传递给web服务调用的初始示例 我做错了什么 我可以用HttpHandlers轻松做到这一点……这么简单的事情不应该这么难吗 更新:将值传递给jQuery';什么是web服务调用?,jquery,web-services,Jquery,Web Services,我正在跟踪各种在线示例,但没有任何成功。我只是尝试创建一个将值传递给web服务调用的初始示例 我做错了什么 我可以用HttpHandlers轻松做到这一点……这么简单的事情不应该这么难吗 更新: 失败的原因是缺少“contentType”属性。报告中概述了这一点 这是我一直遇到的错误: System.InvalidOperationException: Missing parameter: key.\r\n at System.Web.Services.Protocols.ValueCollec
失败的原因是缺少“contentType”属性。报告中概述了这一点 这是我一直遇到的错误:
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()
<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();
}
}
}
这是HTML: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()
<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();
}
}
}
var url='';
函数callWebService(){
jQuery.ajax({
cache:false,
键入:“POST”,
完成:未完成,
数据:“{”键:42}”,
数据类型:“application/json;charset=utf-8”,
错误:onError,
成功:一旦成功,
url:url
});
}
函数onComplete(状态,xmlHttpRequest){
var-stop=“”;
}
函数onError(xmlHttpRequest、状态、错误){
var-stop=“”;
}
函数onSuccess(数据、状态、xmlHttpRequest){
var-stop=“”;
}
jQuery(文档).ready(函数(){
});
这是Web服务: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()
<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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
使用System.Web.Script.Serialization;
命名空间My.Services
{
///
///ProjectDialog的摘要说明
///
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
公共类项目对话框:System.Web.Services.WebService
{
[网络方法]
[ScriptMethod(ResponseFormat=ResponseFormat.Json)]
公共字符串TryThis(Int32键)
{
return key.ToString();
}
}
}
尝试替换以下行:
数据:“{”键:42}”
与:
数据:JSON.stringify(数据)
其中数据是前面声明为的变量:
var data={key:42}
这是从以下答案中得出的:
已更新
正如作者所指出的,上述建议不是问题所在,问题在于contentType设置不正确。但是,上面的链接确实指向了这一点。尝试替换以下行: 数据:“{”键:42}” 与: 数据:JSON.stringify(数据) 其中数据是前面声明为的变量: var data={key:42} 这是从以下答案中得出的: 已更新
正如作者所指出的,上述建议不是问题所在,问题在于contentType设置不正确。然而,上面的链接确实指向了这一点。我认为您的报价可能需要更改
data: '{ "key": 42 }',
应该是
data: "{ 'key': 42 }",
我认为你的报价可能需要改变
data: '{ "key": 42 }',
应该是
data: "{ 'key': 42 }",
请尝试获取并传递“key”作为URL中的参数好吗?您可以尝试获取并传递“key”作为URL中的参数吗?我认为您的问题在于如何设置数据参数,我认为您不需要引号:
data: ({ key: 42 }),
我认为您的问题在于如何设置数据参数,我认为您不需要引号:
data: ({ key: 42 }),
数据的JSON表示形式绝对不需要单引号(撇号),绝对不需要单引号(撇号)感谢您的数据的JSON表示。感谢帮助人员…我会通过注释让大家知道什么有效和什么无效。感谢帮助人员…我会通过注释让大家知道什么有效和什么无效。感谢回复…但是这样做会生成一个相当长的responseText错误,开始是:请求格式无法识别URL意外以“/TryThis”结尾:感谢您的回复…但是这样做会产生一个相当长的responseText错误,以:对于意外以“/TryThis”结尾的URL,请求格式无法识别:您最初的原因实际上不正确…但是引用的文章回答正确。这就是为什么你应该得到学分。你最初的理由实际上是不正确的…但参考文章的答案是正确的。这就是为什么你应该得到学分。