最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to send 'data' to ASMX web service via AJAX POST? - Stack Overflow

programmeradmin5浏览0评论

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.

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);
}

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.

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);
}
Share Improve this question asked Feb 14, 2012 at 21:48 Mr-DCMr-DC 8495 gold badges15 silver badges29 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 12

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

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

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:

[WebService(Namespace = "http://www.somedomainname./")]
[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)
  {
  }
}

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

So you use something like:

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

and jQuery does the rest of the work for you.

A specific example based on a ment:

data: { toSend: "test" }

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

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

发布评论

评论列表(0)

  1. 暂无评论