发布JSON对象到WebService和处理它有 [英] Posting a JSON object to a WebService and Processing it there
问题描述
我创建了以下JSON对象
I am creating the following JSON object
$("#btnSubmit").click(function () {
var poll = { 'q': $('#txtQuestion').val(), 'a': [] };
for (i = 1; i < counter; i++) {
var a = $('#txtAnswer_' + i).val();
poll.a.push(a);
}
$.ajax({
type: 'POST',
url: "../_ws/Polls.asmx",
data: JSON.stringify(poll),
contentType: 'application/json; charset=utf-8',
dataType: 'json'
});
});
基本上,这retreives一个投票的问题,文本框的值与答案文本框(动态创建的)。结果是一个轮询对象
basically, this retreives a question textbox's value and answers textboxes (created on the fly) of a poll. The result is a 'poll' object.
我想从以上可以看出这个对象发送到Web服务。没有问题,直到在这里。我的问题是我怎么retreive在一个函数这个JSON对象和处理它在我的web服务?
I want to send this object to a webservice as seen above. No problems up until here. My question is how do I retreive this json object and process it in a function in my web service?
<WebMethod()> _
<ScriptMethod()>
Public Function AddPoll(**??? as ???**) As String
End Function
我将不胜感激,如果有人可以提供帮助。
I would be grateful if anyone could help.
推荐答案
如果我理解正确的话,调查
最终会变成这样?
If I understand correctly, poll
will end up looking like this?
{ q: 'Question text', a: [ 'Answer 1', 'Answer 2', 'Answer 3' ] }
如果是这样,你可以用一个简单的方法声明会是这样的:
If so, a simple method declaration you could use would be something like:
Public Function AddPoll(q as String, a as List<string>)
如果你想要更多的东西precise,你可以建立自己的一套匹配类型的:
If you wanted something more precise, you could build up your own set of matching type:
public class PollDTO {
public string q { get; set; }
public List<string> a { get; set; }
}
<WebMethod()>
Public Function AddPoll(pollData as PollDTO)
您就需要您的jQuery的数据参数也更改为组单变量也在数据:
You'd need to also change your jQuery data parameter to group that data under the single variable also:
data: JSON.stringify({ pollData: poll })
更多关于这里:的http:// encosia.com/using-complex-types-to-make-calling-services-less-complex/
这篇关于发布JSON对象到WebService和处理它有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!