0

I'm trying pushing json inside of array, but when i send the params with Ajax it put 0 instead of Array...

Not really sure how do i get rid of it.. If anyone have any idea how to fix that I'll really appreciate it!

  var $selector = $(this).find('.button-selector');
  var $element = $(this).find('.button-type');
  var $attr = $(this).find('.selector ul li.active').attr('type');

  var $text = $(this).find('textarea').val();
  var $buttons = [];

  $selector.each(function(){
    var $titleBtn = $(this).find('input[name=button-title]').val();

    if($attr == 'block'){
      var $payload = "";

      var $value = $element.find('.block-select select').val();
      if($value == 1) {
        $payload = "JOIN_CONVERSATION_"+story_id+"";
      } else if ($value == 2) {
        $payload = "NEXT_BITE_"+story_id+"";
      } else if ($value == 3) {
        $payload = "INSTANT_ARTICLE_"+story_id+"";
      }

      button = {
        "type": "postback",
        "title": $titleBtn,
        "payload": $payload
      };

      $buttons.push(button)

    } else if($attr == 'url') {
      var $url = $element.find('.url-select input[type=url]').val();
      var $webView = $element.find('.url-select select').val();

      button = {
        "type": "web_url",
        "url": $url,
        "title": $titleBtn,
        "webview_height_ratio": $webView
      };

      $buttons.push(button)
    }
  });

  $.ajax({
    async: false,
    type: 'POST',
    url: '/variable',
    dataType: 'json',
    data: {
      "element":{
        "notification_type": notification_type,
        "message": {
          "attachment": {
           "type": "template",
           "payload": {
             "template_type": "button",
             "text": $text,
             "buttons": $buttons
           }
          }
        }
      }
    }
  });

The output for buttons is:

"buttons"=>{"0"=>{"type"=>"postback", "title"=>"rwerwe", "payload"=>"JOIN_CONVERSATION_256"}}

Intead of:

"buttons"=>[{"type"=>"postback", "title"=>"rwerwe", "payload"=>"JOIN_CONVERSATION_256"}]
5
  • Just a note, you should really only use $ as a prefix for variables if they are a JQuery object/array. This is unrelated to any issues, just a suggestion for you moving forward. Commented Dec 3, 2016 at 1:48
  • You don't need to change that. You can still access buttons[0] right? and you can loop into it also just like a regular array... even if it looks like an object. Commented Dec 3, 2016 at 1:48
  • What is the $buttons value before the ajax call? Commented Dec 3, 2016 at 1:49
  • @Santi: Alright, thanks for the note i keep it on mind.. @barudo: The problem is, im sending it out, it's not accepting it like that @Aruna: Not sure what u mean.. its a variable and its $buttons (array) Commented Dec 3, 2016 at 1:51
  • @LiborZahrádka Just above the $.ajax({ line, if you print console.log($buttons), is it the same with 0? Commented Dec 3, 2016 at 1:53

1 Answer 1

1

I hope this should fix your case.

You need to use JSON.stringify to first properly serialize your object to JSON, and then specify the contentType: "application/json" so your server understands it's JSON and deserialize it back. This should do the trick:

var jsonData = {
      "element":{
        "notification_type": notification_type,
        "message": {
          "attachment": {
           "type": "template",
           "payload": {
             "template_type": "button",
             "text": $text,
             "buttons": $buttons
           }
          }
        }
      }
    };

$.ajax({
    async: false,
    type: 'POST',
    url: '/variable',
    contentType: 'application/json',
    dataType: 'json',
    data: JSON.stringify(jsonData)
  });
Sign up to request clarification or add additional context in comments.

11 Comments

Sorry i didn't mentioned but i tried that and the problem is that its returning different JSON structure "buttons"=>"[{\"type\":\"postback\",\"title\":\"rwerwe\",\"payload\":\"JOIN_CONVERSATION_256\"}]" intead of "buttons"=>[{"type"=>"postback", "title"=>"rwerwe", "payload"=>"JOIN_CONVERSATION_256"}]
Yes, that's the serialized json string, you can check this in your server and should be deserialized back correctly.
Not sure, but when i send it to API its not responding so i can't use that structure
Which API it is, third party or your own API? Otherwise try this without JSON.stringify as data: jsonData but with contentType: 'application/json'.
its facebook and when i use contentType: 'application/json' im getting error 400 bad request
|

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.