ASP.Net JSON Web服务发布表单数据 [英] ASP.Net JSON Web Service Post Form Data

查看:110
本文介绍了ASP.Net JSON Web服务发布表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有饰有 System.Web.Script.Services.ScriptService(),以便它可以返回JSON格式的数据的ASP.NET Web服务。这多少是为我工作,但ASP.Net有参数,Web服务必须以JSON为了得到JSON了要求。

I have a ASP.NET web service decorated with System.Web.Script.Services.ScriptService() so it can return json formatted data. This much is working for me, but ASP.Net has a requirement that parameters to the web service must be in json in order to get json out.

我使用jQuery运行我的Ajax调用和似乎没有要创建一个简单的方式的很好的从表单元素中的JavaScript对象。我已经看过serialiseArray在json2库,但它没有连接code中的字段名作为对象属性的名称。

I'm using jquery to run my ajax calls and there doesn't seem to be an easy way to create a nice javascript object from the form elements. I have looked at serialiseArray in the json2 library but it doesn't encode the field names as property name in the object.

如果你有2个表单元素像这样

If you have 2 form elements like this

 <input type="text" name="namefirst" id="namefirst" value="John"/>
 <input type="text" name="namelast" id="namelast" value="Doe"/>

调用的 $(形式)。序列化()会得到你的标准的查询字符串

calling $("form").serialize() will get you the standard query string

namefirst=John&namelast=Doe

调用 JSON.stringify($(形式)。serializeArray())将让你的(大件)JSON重新presentation

calling JSON.stringify($("form").serializeArray()) will get you the (bulky) json representation

[{"name":"namefirst","value":"John"},{"name":"namelast","value":"Doe"}]

,你必须有code这样在读它这将传递到Web服务时,但其丑陋的工作:

This will work when passing to the web service but its ugly as you have to have code like this to read it in:

Public Class NameValuePair
    Public name As String
    Public value As String
End Class
<WebMethod()> _
Public Function GetQuote(ByVal nvp As NameValuePair()) As String

End Function

您也必须换另一个对象nameed NVP内部的JSON文本,使Web服务满意。然后,当你想要一个关联数组,它更多的工作,因为你已经是的NameValuePair数组。

You would also have to wrap that json text inside another object nameed nvp to make the web service happy. Then its more work as all you have is an array of NameValuePair when you want an associative array.

我可能是在开玩笑自己,但我想象中的东西更优雅,当我开始这个项目 - 更多像这样

I might be kidding myself but i imagined something more elegant when i started this project - more like this

Public Class Person
    Public namefirst As String
    Public namelast As String
End Class

这将需要JSON来是这个样子:

which would require the json to look something like this:

 {"namefirst":"John","namelast":"Doe"}

有没有一种简单的方法来做到这一点?显然,这是简单的双参数形式,但,当你有一个非常大的形式连接字符串变得难看。其嵌套对象也将复杂的事情

Is there an easy way to do this? Obviously it is simple for a form with two parameters but when you have a very large form concatenating strings gets ugly. Having nested objects would also complicate things

我已经定居在了那一刻的cludge是使用JSON对象里面鼓鼓囊囊的标准名称值对格式。这是紧凑和快速

The cludge I have settled on for the moment is to use the standard name value pair format stuffed inside a json object. This is compact and fast

{"q":"namefirst=John&namelast=Doe"}

再有分析查询字符串转换成副阵列的服务器在这样一个Web方法。

then have a web method like this on the server that parses the query string into an associate array.

<WebMethod()> _
Public Function AjaxForm(ByVal q As String) as string
    Dim params As NameValueCollection = HttpUtility.ParseQueryString(q)
    'do stuff
    return "Hello"
End Sub

据一个cludges去code量而言,这一次似乎相当优雅,但我的问题是:有没有更好的办法?是否有表单数据传递到asp.net网站/脚本服务的普遍接受的方式?

As far a cludges go this one seems reasonably elegant in terms of amount of code, but my question is: is there a better way? Is there a generally accepted way of passing form data to asp.net web/script services?

推荐答案

我没有带发现任何东西比我已经与传递序列化的字符串作为参数想好。

I havn't found anything better than what i was already thinking with passing the serialized string as a parameter.

输入:

  {"q":"namefirst=John&namelast=Doe"}

Web服务:

<WebMethod()> _
Public Function AjaxForm(ByVal q As String) as string
    Dim params As NameValueCollection = HttpUtility.ParseQueryString(q)
    'do stuff
    return "Hello"
End Sub

这似乎是最清洁,最简单的选项

This seems the cleanest and simplest option

这篇关于ASP.Net JSON Web服务发布表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆