从jQuery获取JSON对象到。net最简单的方法是什么?

时间:2022-06-18 14:04:43

I'm working on an asp.net/jQuery app that needs to communicate to the server.

我正在开发一个需要与服务器通信的asp.net/jQuery应用程序。

The page features a series of checkboxes, and I would like to configure jQuery to tell the server a simple message like this

该页面具有一系列复选框,我想配置jQuery来告诉服务器这样一个简单的消息

"checkBox ID 12 is checked"

"复选框ID 12被选中"

That will turn into simple Json

这将变成简单的Json

{
     "ID": "12",
     "Checked" : "true"
}

I don't think I need to get into RIA Services, at least I hope not. All I want to do is keep a record of what has been checked on the server in case they close the browser and come back later.

我不认为我需要进入RIA服务,至少我希望不是。我所要做的就是记录在服务器上检查过的内容,以防他们关闭浏览器之后回来。

What is the best technique to achieve this with minimal code? WCF?

用最少的代码达到这个目的的最佳技术是什么?WCF吗?

Thanks, John

谢谢你,约翰

3 个解决方案

#1


1  

ASMX ScriptServices (or ASPX's page methods if you want to keep this code with a particular page's code) are a great way to do that without succumbing to WCF's complexity. If you use jQuery's $.ajax() to send JSON from the client-side that matches a class on the server-side, ASP.NET will handle the mapping between JSON and that object automatically: http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

ASMX ScriptServices(或ASPX的页面方法,如果您希望将此代码与特定页面的代码保持在一起)是一种很好的方法,而不会屈服于WCF的复杂性。如果您使用jQuery的$.ajax()从与服务器端类匹配的客户端发送JSON,那么请使用ASP。NET将自动处理JSON和该对象之间的映射:http://encosia.com/using-complex-types-to-make-call -services-less complex/

In fact, if you end up needing to deal with DateTimes, Dictionaries, or enums in JSON, ASMX's underlying serializer is better than WCF's.

事实上,如果您最终需要处理JSON格式的日期时间、字典或枚举,ASMX的底层序列化器比WCF的要好。

In the long run, Web API is going to hit the sweet spot between ASMX's simplicity and WCF's additional features. In my experimentation, moving an ASMX service to Web API is extremely easy though. So, you can take the easy option today and not worry about painting yourself into a corner if ASMX is deprecated in ASP.NET 5 (however, ASMX is still in the Add New Item list as "Web Service" in the Visual Studio v.Next developer preview, so you really have to take talk about its imminent deprecation with a grain of salt).

从长远来看,Web API将在ASMX的简单性和WCF的附加特性之间找到一个平衡点。在我的实验中,将ASMX服务迁移到Web API非常容易。因此,如果ASMX在ASP中被弃用,您可以选择今天的简单选项,而不用担心把自己画到一个角落。然而,ASMX仍然在Visual Studio v中作为“Web服务”添加新项列表中。下一个开发者预览,所以你真的必须要谈论它即将到来的弃用一粒盐)。

#2


0  

Not sure how your webpage is created...I have recently created a page that was dynamically driven and I pass in a JSON string representation of the JSON object in the background, and then on page load take that JSON string and turn it into a JSON object. Then when the user makes changes, in the background, I then manipulate that JSON object and send it to the server and pass it along from there.

不知道你的网页是如何创建的……我最近创建了一个动态驱动的页面,我将JSON对象的JSON字符串表示在后台,然后在页面加载中使用JSON字符串并将其转换为JSON对象。然后,当用户在后台进行更改时,我将操作该JSON对象并将其发送到服务器,并将其传递给服务器。

I guess you could create a JSON object on the fly, whenever the a checkbox is selected and then make an ajax call via jquery. Now each time you select a checkbox does that need to add to the JSON object or create its own individual object?

我猜您可以动态地创建一个JSON对象,无论何时选择复选框,然后通过jquery进行ajax调用。现在,每当您选择一个复选框时,是否需要添加到JSON对象或创建它自己的单独对象?

Why do you need a JSON object if your just wanting to tell the server that an object has been selected?

如果您只想告诉服务器一个对象已经被选中,为什么需要一个JSON对象?

#3


0  

I found the best way was to use a combination of jQuery $.post() and a generic handler that pulls the values out of the Request.Form collection, so I don't need to get involved in WCF at all.

我发现最好的方法是使用jQuery $.post()和从请求中提取值的通用处理程序的组合。表单收集,所以我根本不需要参与WCF。

#1


1  

ASMX ScriptServices (or ASPX's page methods if you want to keep this code with a particular page's code) are a great way to do that without succumbing to WCF's complexity. If you use jQuery's $.ajax() to send JSON from the client-side that matches a class on the server-side, ASP.NET will handle the mapping between JSON and that object automatically: http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

ASMX ScriptServices(或ASPX的页面方法,如果您希望将此代码与特定页面的代码保持在一起)是一种很好的方法,而不会屈服于WCF的复杂性。如果您使用jQuery的$.ajax()从与服务器端类匹配的客户端发送JSON,那么请使用ASP。NET将自动处理JSON和该对象之间的映射:http://encosia.com/using-complex-types-to-make-call -services-less complex/

In fact, if you end up needing to deal with DateTimes, Dictionaries, or enums in JSON, ASMX's underlying serializer is better than WCF's.

事实上,如果您最终需要处理JSON格式的日期时间、字典或枚举,ASMX的底层序列化器比WCF的要好。

In the long run, Web API is going to hit the sweet spot between ASMX's simplicity and WCF's additional features. In my experimentation, moving an ASMX service to Web API is extremely easy though. So, you can take the easy option today and not worry about painting yourself into a corner if ASMX is deprecated in ASP.NET 5 (however, ASMX is still in the Add New Item list as "Web Service" in the Visual Studio v.Next developer preview, so you really have to take talk about its imminent deprecation with a grain of salt).

从长远来看,Web API将在ASMX的简单性和WCF的附加特性之间找到一个平衡点。在我的实验中,将ASMX服务迁移到Web API非常容易。因此,如果ASMX在ASP中被弃用,您可以选择今天的简单选项,而不用担心把自己画到一个角落。然而,ASMX仍然在Visual Studio v中作为“Web服务”添加新项列表中。下一个开发者预览,所以你真的必须要谈论它即将到来的弃用一粒盐)。

#2


0  

Not sure how your webpage is created...I have recently created a page that was dynamically driven and I pass in a JSON string representation of the JSON object in the background, and then on page load take that JSON string and turn it into a JSON object. Then when the user makes changes, in the background, I then manipulate that JSON object and send it to the server and pass it along from there.

不知道你的网页是如何创建的……我最近创建了一个动态驱动的页面,我将JSON对象的JSON字符串表示在后台,然后在页面加载中使用JSON字符串并将其转换为JSON对象。然后,当用户在后台进行更改时,我将操作该JSON对象并将其发送到服务器,并将其传递给服务器。

I guess you could create a JSON object on the fly, whenever the a checkbox is selected and then make an ajax call via jquery. Now each time you select a checkbox does that need to add to the JSON object or create its own individual object?

我猜您可以动态地创建一个JSON对象,无论何时选择复选框,然后通过jquery进行ajax调用。现在,每当您选择一个复选框时,是否需要添加到JSON对象或创建它自己的单独对象?

Why do you need a JSON object if your just wanting to tell the server that an object has been selected?

如果您只想告诉服务器一个对象已经被选中,为什么需要一个JSON对象?

#3


0  

I found the best way was to use a combination of jQuery $.post() and a generic handler that pulls the values out of the Request.Form collection, so I don't need to get involved in WCF at all.

我发现最好的方法是使用jQuery $.post()和从请求中提取值的通用处理程序的组合。表单收集,所以我根本不需要参与WCF。