jQuery Ajax显示0而不是Array

时间:2016-12-03 01:44:22

标签: javascript jquery arrays ajax

我尝试在数组中推送json,但是当我用Ajax发送params时,它将0放入而不是Array ...

我不确定如何摆脱它.. 如果有人知道如何解决这个问题,我会非常感激!

  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
           }
          }
        }
      }
    }
  });

按钮的输出是:

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

Intead of:

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

0 个答案:

没有答案