0

How can I pass a Array via JQuery Get without using post

my jquery code is

$('[name="SelectHighlights"]:checked').each(function () {
    var row = $(this).closest('tr');

    var high = {
        AccountName: row.find('td:nth-child(2)').text(),
        Highcomments: row.find('td:nth-child(3) > input').val()
    };
    HighlightsArea.push(high);
});

var HL=HighlightsArea.length;
alert(''+HL);
if(HL>0)
{
    GetJson(
        GetRootPath() + '/WeeklySales/AjaxUpdateHighComments', //url
        //JSON.stringify(HighlightsArea), //Data: 
        //HighlightsArea,
        //{high: HighlightsArea},
        {array: HighlightsArea.join(",")},
        SaveChanges //callBack
    );

}

Function for GetJSon:

function GetJson(url, data, onSuccess) {
    $.ajax({
        url: url,
        data: data,
        success: onSuccess,
        error: GenericErrorHandler,
        traditional: true,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'GET'
    });
}

code in my controller :

I want to pass that array into this controller only but i should not use HTTpPost

public ActionResult AjaxUpdateHighComments(List<Highlights> Highlights, WeeklySalesModel weeklysales){}

Please help me Any kind of help is highly appreciated. thanks in advance.

1
  • 1
    why can't you use a post? Commented May 7, 2013 at 5:30

3 Answers 3

1

I suggest you must convert to string first your JSON Array on Passing data.

Client-side

function GetJson(url, data, onSuccess) {
    $.ajax({
        url: url,
        data: JSON.stringify(data), // converted to string
        success: onSuccess,
        error: GenericErrorHandler,
        traditional: true,
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        type: 'GET'
    });
}

Try this code to your

Controller

[HttpGet]
public ActionResult AjaxUpdateHighComments(string highlights)
{
    JavaScriptSerializer jss = new JavaScriptSerializer();
    List<Highlights> listHighlights = jss.Deserialize<List<Highlights>>(highlights);

 ...
Sign up to request clarification or add additional context in comments.

Comments

0

You're a bit ambiguous in your question but i'll give it a go, assuming you mean you want to prevent posting to the Action?

You can add a attribute on the action to prevent this MSDN HttpGetAttribute

for example

[HttpGet]
public ActionResult AjaxUpdateHighComments(List<Highlights> Highlights, WeeklySalesModel weeklysales){}

Comments

0

First of all, include the $.postify code from this article.

Code for $.postify:

$.postify = function(value) {
    var result = {};

    var buildResult = function(object, prefix) {
        for (var key in object) {

            var postKey = isFinite(key)
                ? (prefix != "" ? prefix : "") + "[" + key + "]"
                : (prefix != "" ? prefix + "." : "") + key;

            switch (typeof (object[key])) {
                case "number": case "string": case "boolean":
                    result[postKey] = object[key];
                    break;

                case "object":
                    if (object[key].toUTCString)
                        result[postKey] = object[key].toUTCString().replace("UTC", "GMT");
                    else {
                        buildResult(object[key], postKey != "" ? postKey : key);
                    }
            }
        }
    };

    buildResult(value, "");

    return result;
};

I know the name sounds like it is for POST but it works for GET as well. Then change your code as below:

$('[name="SelectHighlights"]:checked').each(function () {
    var row = $(this).closest('tr');

    var high = {
        AccountName: row.find('td:nth-child(2)').text(),
        Highcomments: row.find('td:nth-child(3) > input').val()
    };
    HighlightsArea.push(high);
});

var HL = HighlightsArea.length;
alert('' + HL);
if (HL > 0) {    
    //Property names should match the parameters' name given in the controller method
    var objRequest = {
        Highlights: HighlightsArea,
        weeklysales: {/* weekly sales object goes here */}
    };    
    GetJson(
    GetRootPath() + '/WeeklySales/AjaxUpdateHighComments',
    $.postify(objRequest), // $.postify is the key
    SaveChanges //callBack
    );
}

7 Comments

Thank you very much can you please let me know whther i need to add any plugin fir this.
Other than jQuery (which you already have), you just need the $.postify code given in the article/link i've provided. For your convenience i've updated my answer with the code.
Thanks a lot it is working very very fine but only thing is in the code i gave above Highcomments is value from text box ,i am not getting that value in controller,it is showing null can you please help me
What about AccountName? Are you getting the value/text for that in the controller?
In that case, this line row.find('td:nth-child(3) > input').val() is not working as intended. Can you post a piece/snippet of the html output?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.