Send array via Ajax

0

I need to send an array using an ajax call.

$.ajax({
    type: "GET",
    crossDomain: true,
    dataType: "json",
    url: "http://localhost:24234/api/SendPushNotification",
    success: {
        Function(result) {
            Console.log(result);
        }
    },
    data: {
        'user_id': '1',
        'body_param': 'body',
        'title_param': 'title'      
    },
    headers: { 
        'Token': 'ccTtUKQS32PjzMYQEzH2OUQN8nJpsCUXg7iWRYA7cEpAJLoNEWCKr2NcQyFUbdV4' 
    }
});

How could it be done?

PS: where the user id goes, there will be an "unlimited number of users according to internal filters.

As a VB server language. It is for a webservice that is responsible for sending notifications. Currently it works by sending identifiers one by one. But it is unfeasible considering what it takes to make the call to the ws etc ...

    
asked by GDP 23.11.2017 в 15:21
source

1 answer

2

Good day to make an ajax query it is necessary that you use the tag 'data' in the items that you are going to parameterize when making the request, it would be something like the following:

var arr = ["Saab", ["Volvo", "Volvo2","Volvo3"], "BMW"];

var valParam = JSON.stringify(arr);

$.ajax({
        url: 'TuURL',
        type: 'POST',
        data: { tuArrJson: valParam} ,
        contentType: 'application/json; charset=utf-8',
        success: function (response) {
            alert(response.status);
        },
        error: function () {
            alert("error");
        }
    }); 

on the server side you pick up the value with Request and then read the value that arrives in the following way:

$param = json_decode($_REQUEST['tuArrJson']);

  foreach($param  as $val){
     echo "<br> - valor: ".$val;
  }

As it is in VB it would be to iterate with two loops as I show you next:

Dim numbers = {{1, 2}, {3, 4}, {5, 6}}

For index0 = 0 To numbers.GetUpperBound(0)
    For index1 = 0 To numbers.GetUpperBound(1)
        Debug.Write(numbers(index0, index1).ToString & " ")
    Next
    Debug.WriteLine("")
Next

I hope it will be useful for you. Greetings.

    
answered by 23.11.2017 / 16:37
source