Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize jQuery Serialized Form

I'm trying to pass form inputs into a WebMethod and doing something. I used jQuery Serialize.

<script type="text/javascript">
        $.fn.serializeNoViewState = function () {
            return this.find("input,textarea,select,hidden")
               .not("[type=hidden][name^=__]")
               .serialize();
        }

        $(function () {
            $("#Button1").click(function (e) {
                var res = $("#myform").serializeNoViewState();
                var jsonText = JSON.stringify({ bject: res });
                $.ajax({
                    type: "POST",
                    url: "Default.aspx/Test",
                    data: jsonText,
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (msg) {
                    //    alert("asd");


                    },
                    error: AjaxFailed
                });
            });
        });
        function AjaxFailed(result) {
            alert("Failed");
        }  
    </script>

in target WebMethod I want to Deserialize that object I passed.

[WebMethod()]
public static bool test(string bject)
{
    JavaScriptSerializer JsTool = new JavaScriptSerializer();

}

I Tried to use Javascriptserilizer Class. but I did not succeed. now how can I use this Object? I want to use this way for using jQuery AJAX simpler(For example passing form inputs to a WebService and inserting that in Database). Due the action I want to do is this way right ? Welcome your Suggestions , tips .

Update:

how can I map the Serialized JS object to my C# entity object? Is this way is good Way ? or there are better way exist ? if yes please give me some information

like image 673
Shahin Avatar asked Apr 07 '11 20:04

Shahin


People also ask

How do I PHP Unserialize a jQuery serialized form?

Your answer something like this is probably all you need: $params = array(); parse_str($_GET, $params); $params should then be an array modeled how you would expect. Note this works also with HTML arrays.

What is serialize deserialize?

Serialization is the process of converting an object into a stream of bytes to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

What is serialize () in jQuery?

jQuery serialize() Method The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.

How can I access serialized form data in PHP?

To get the POST values from serializeArray in PHP, use the serializeArray() method. The serializeArray( ) method serializes all forms and form elements like the . serialize() method but returns a JSON data structure for you to work with.


1 Answers

I would recommend you working with strong types. So define a class that will contain all the necessary properties:

public class MyModel
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

and then have your web method take this object:

[WebMethod()]
public static bool test(MyModel bject)
{
    ...
}

The name of the properties must match the input field names you are serializing in the AJAX request.

like image 53
Darin Dimitrov Avatar answered Sep 30 '22 14:09

Darin Dimitrov