如何使用jquery将json数据发送到asmx(从aspx)?
我在 aspx 3.5 中构建了联系表单,并使用 jQuery 将其发送到 Web 服务 (asmx)。
Web服务需要返回成功或错误代码。问题是在网络方法中我只得到单个值而不是数组。我是 ajax 新手,尝试了很多解决方案,但没有任何结果。如果您只能向我解释一下做什么的原理,那也很好。
这是客户端:
$(document).ready(function()
{
$("#submit").click(function(event)
{
$.ajax
({
type: "POST",
url: "RVContactFormMailer.asmx/HelloToYou",
data: "{'name': '" + $('#name').val() + "', 'company':'" + $('#company').val() + "', 'phone':'" + $('#phone').val() + "', 'email':'" + $('#email').val() + "', 'questions':'" + $('#questions').val() + "'}" ,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
AjaxSucceeded(msg);
}, error: AjaxFailed
});
});
在 firebug 中,它正确发送:
{'name': 'jhon', 'company':'example', 'phone':'123', 'email':'[email protected]', 'questions':'hello'}
asmx 代码是(请忽略名称,其示例:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService] // To allow this Web Service to be called from script, using ASP.NET AJAX or JQuery.
[ToolboxItem(false)]
public class RVContactFormMailer : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloToYou(string name)
{
return "Hello " + name;
}
}
当我调试时,我看到输入参数“name”仅包含一个字符串 - 我不知道如何获取我发送到服务并包含所有表单数据的完整 json 字符串 - 我想将其反序列化为字符串数组或类似的东西,并处理它。 我怎样才能做到这一点?
I built contact form in aspx 3.5 and I'm using jQuery to send it to web service (asmx).
The web service need to return success or error code. The problem is that at the web method I get only single value and not array. I'm kind of new in ajax and I tried a lot of solutions but without any results. Please if you can only explain me the principle of what to do it also be good.
This is the client side:
$(document).ready(function()
{
$("#submit").click(function(event)
{
$.ajax
({
type: "POST",
url: "RVContactFormMailer.asmx/HelloToYou",
data: "{'name': '" + $('#name').val() + "', 'company':'" + $('#company').val() + "', 'phone':'" + $('#phone').val() + "', 'email':'" + $('#email').val() + "', 'questions':'" + $('#questions').val() + "'}" ,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
AjaxSucceeded(msg);
}, error: AjaxFailed
});
});
In firebug its sends correctly:
{'name': 'jhon', 'company':'example', 'phone':'123', 'email':'[email protected]', 'questions':'hello'}
The asmx code is (please ignore the names, its example:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService] // To allow this Web Service to be called from script, using ASP.NET AJAX or JQuery.
[ToolboxItem(false)]
public class RVContactFormMailer : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloToYou(string name)
{
return "Hello " + name;
}
}
When I debug I see that the input parameter "name" contains only one string - I don't know how to get the full json string that I send to the service and contains all the form data - I want to desirialize it to string array or something like, and process it.
how can I do that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题不在客户端 - 它在服务器端 - 问题是我向网络服务发送了几个参数,但该函数只得到一个:
而正确的应该是:
无论如何,感谢帮助!
the problem was not in the client side - it was in the server side - the problem is that i send few parameters to the web service but the function get only one:
while the correct one should be:
anyway, Thanks for the help!
您是否尝试过查看 request.form 集合?由于您正在发出发布请求并将参数作为数据传递给请求,因此它将在 Request.Form 中可用。
did you tried looking at request.form collection? since you are making post request and passing the params as a data to the request, it would be available in Request.Form.