javascript 如何通过 AJAX POST 将“数据”发送到 ASMX Web 服务?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9284680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:10:51  来源:igfitidea点击:

How to send 'data' to ASMX web service via AJAX POST?

javascriptajaxpost

提问by davey1990

I can successfully receive values from my web service so in that repect the script is working fine. However I am now trying to send data to the webservice using the 'data' field below. What i cant figure out is how to send a simple string (e.g. "test") to the web service which is what my web method expects as a parameter.

我可以成功地从我的网络服务接收值,因此脚本工作正常。但是,我现在正在尝试使用下面的“数据”字段将数据发送到网络服务。我无法弄清楚的是如何将一个简单的字符串(例如“测试”)发送到 Web 服务,这是我的 Web 方法所期望的参数。

Any help is much appreciated. For example:

任何帮助深表感谢。例如:

function setQuestion() {
$.ajax({
    type: "POST",
    **data: "{}",** //how do i use this to send a string??
    dataType: "json",
    url: "http://someURL",
    contentType: "application/json; charset=utf-8",
    success: onSuccess
});
}

function onSuccess(msg) {
$("#questiontxt").append(msg);
}

回答by cdm9002

For asmx you need to pass a stringified version of the data object, so for example:

对于 asmx,您需要传递数据对象的字符串化版本,例如:

var data = "{param1:" + param1IsANumber +
           ", param2:\"" + param2IsAString + "\"}";
$.ajax({
 data: data,
 dataType: "json",
 url: url,
 type: "POST",
 contentType: "application/json; charset=utf-8",
 success: function (result) {}
});

Or you can hava an object and use jquery-json

或者你可以拥有一个对象并使用jquery-json

var data = {};
data.param1 = 1;
data.param2 = "some string";
$.ajax({
 data: jQuery.toJSON(data),
 dataType: "json",
 url: url,
 type: "POST",
 contentType: "application/json; charset=utf-8",
 success: function (result) {}
});

Finally, your web service class must look like:

最后,您的 Web 服务类必须如下所示:

[WebService(Namespace = "http://www.somedomainname.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class MyService : System.Web.Services.WebService
{
  [WebMethod]
  [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
  public void MyServiceCall(int param1, string param2)
  {
  }
}

回答by Dave

jQuery takes the data argument and converts it into the appropriate type of request variables.

jQuery 获取数据参数并将其转换为适当类型的请求变量。

So you use something like:

所以你使用类似的东西:

data: { myParameterName: "myParameterValue", myParameterName2: "myParameterValue2" }

and jQuery does the rest of the work for you.

jQuery 会为您完成剩下的工作。

A specific example based on a comment:

基于评论的具体示例:

data: { toSend: "test" }

回答by petrov.alex

data: "{"parameterName": "test"}"

data: "{"parameterName": "test"}"

in WebService: public void GetData(string parameterName) {}

在网络服务中: public void GetData(string parameterName) {}