0

I have the following array object

[{"site":"88333","event":"TEST","event_window":"4th April - 4th April","monitoring_start":"10-Apr-2016","monitoring_end":"10-Apr-2016"}]

I am trying to pass on ajax post as key value. I can have multiple rows. I have tried $.param(JSON.stringify(json_o)) and $.param(json_o), which does not work

Example shown here looks like array object which I have done but does not work.

var params = JSON.stringify(json_o);
        console.log(params);
        $.ajax({
            url : '../../api/netcool/add-event',
            data : params,
            type: 'POST',
            error : function (data, textStatus, jqXHR) {
                if(data.statusText != "abort")
                    console.log(data);
            },
            dataType : 'json',
            success : function (data) { 
                console.log(data);
            }
        });
6
  • 2
    If you're using jQuery, you don't have to do anything, just send the objects as data in your $.ajax request. Commented Apr 4, 2016 at 16:53
  • not sure what you mean? Commented Apr 4, 2016 at 16:53
  • 1
    $.ajax({ url : 'someurl', data : your_array[0] }) Commented Apr 4, 2016 at 16:53
  • I want to pass everything in array as I can have multiple sites Commented Apr 4, 2016 at 16:54
  • 1
    How the data needs to be encoded also depends on what the server expects. Commented Apr 4, 2016 at 16:56

1 Answer 1

4

Try this..

var params = [{"site":"88333","event":"TEST","event_window":"4th April - 4th April","monitoring_start":"10-Apr-2016","monitoring_end":"10-Apr-2016"}];
    console.log(params);
    $.ajax({
        url : '../../api/netcool/add-event',
        data : {params:params},
        type: 'POST',
        error : function (data, textStatus, jqXHR) {
            if(data.statusText != "abort")
                console.log(data);
        },
        success : function (data) { 
            console.log(data);
        }
    });

And in ../../api/netcool/add-event method.. you can acccess the parameter by

print_r($_REQUEST['params'][0]);
echo $_REQUEST['params'][0]['site'];
Sign up to request clarification or add additional context in comments.

Comments

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.