简体   繁体   English

ASP.Net JSON Web服务发布表格数据

[英]ASP.Net JSON Web Service Post Form Data

I have a ASP.NET web service decorated with System.Web.Script.Services.ScriptService() so it can return json formatted data. 我有一个装饰有System.Web.Script.Services.ScriptService()的ASP.NET Web服务,因此它可以返回json格式的数据。 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. 这对我来说很有效,但是ASP.Net要求Web服务的参数必须在json中才能获取json。

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. 我正在使用jquery运行我的ajax调用,似乎没有一种简单的方法可以从form元素创建漂亮的 javascript对象。 I have looked at serialiseArray in the json2 library but it doesn't encode the field names as property name in the object. 我已经看过json2库中的serialiseArray,但是它没有将字段名称编码为对象中的属性名称。

If you have 2 form elements like this 如果您有2个这样的表单元素

 <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 调用$(“ form”)。serialize()将为您提供标准查询字符串

namefirst=John&namelast=Doe

calling JSON.stringify($("form").serializeArray()) will get you the (bulky) json representation 调用JSON.stringify($(“ form”)。serializeArray())会得到(笨重的)json表示形式

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

This will work when passing to the web service but its ugly as you have to have code like this to read it in: 当传递给Web服务时,这将起作用,但是它很丑陋,因为您必须具有如下代码才能读取它:

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

You would also have to wrap that json text inside another object nameed nvp to make the web service happy. 您还必须将json文本包装在另一个名为nvp的对象中,以使Web服务满意。 Then its more work as all you have is an array of NameValuePair when you want an associative array. 然后,当您需要关联数组时,它所要做的工作更多,因为只有NameValuePair数组。

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

which would require the json to look something like this: 这将要求json看起来像这样:

 {"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 嵌套对象也会使事情复杂化

The cludge I have settled on for the moment is to use the standard name value pair format stuffed inside a json object. 我目前解决的难题是使用填充在json对象中的标准名称值对格式。 This is compact and fast 这是紧凑而快速的

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

then have a web method like this on the server that parses the query string into an associate array. 然后在服务器上使用类似Web的方法,将查询字符串解析为关联数组。

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

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? 是否存在将表单数据传递到asp.net Web /脚本服务的公认方法?

You are just having a formatting crisis. 您正面临格式危机。

To properly call this code: 正确调用此代码:

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

You need to send a json string that looks like this: 您需要发送一个如下所示的json字符串:

'{"nvp": {"name": "john", "value": "foo"}}'

Do not use jQuery to serialize ScriptService arguments. 不要使用jQuery序列化ScriptService参数。 Use the standard json2.js . 使用标准的json2.js

Try this: Form 试试这个:表格

...
 <input type="text" name="name" id="name" value="John"/>
 <input type="text" name="value" id="value" value="Foo"/>
...

Script: 脚本:

var myNvp = {name: $('#name').val(), value:$('#value').val()};
var data = JSON.stringify({nvp: myNvp});
// data is what you post to the service.

I just wrote this off the top of my head but it looks right to me. 我只是在脑海中写下了这个,但对我来说看起来不错。

Let me know if you have any other questions. 如果您还有其他问题,请告诉我。

I havn't found anything better than what i was already thinking with passing the serialized string as a parameter. 我没有发现比将序列化字符串作为参数传递给我的想法更好的东西。

input: 输入:

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

webservice: 网络服务:

<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 这似乎是最干净,最简单的选择

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM