weixin_33711647 2017-10-28 17:40 采纳率: 0%
浏览 11

来自ajax的jQuery变量

I have a javascript function that has an array variable. I would like to get this array from PHP side. But I could not do it. Can anyone help me to how to get labels from the php side? I looked other questions but most questions are regarding to passing jquery to php. my one is reverse side.

   $form_card.on('click', function(e) {
            if(!$form_card.hasClass('card-expanded')) {
                var template = Handlebars.compile($note_form_template),
                    context = {
                        'labels': [
                            {
                                'text': 'label 1',
                                'text_safe': 'label_1',
                                'type': 'default'
                            },
                            {
                                'text': 'label 2',
                                'text_safe': 'label_2',
                                'type': 'warning'
                            },
                            {
                                'text': 'label 3',
                                'text_safe': 'label_3',
                                'type': 'danger'
                            },
                            {
                                'text': 'label 4',
                                'val': 'label_4',
                                'type': 'success'
                            },
                            {
                                'text': 'label 5',
                                'text_safe': 'label_5',
                                'type': 'primary'
                            }
                        ]
                    };
                    html = template(context);

                $form.hide().html(html);
                $note_form_text.hide();

Here is my ajax function...

    $.ajax({
    type: 'GET',
    url: SITE_URL + 'nsys/dashboard/noteLabels',
    //dataType: 'json',
    success: function (result) {
       var res = JSON.parse(result);
    },
});
return res;

}

and this is my php side ...

 public function noteLabels()
{
    $labels = array(
        0 => array(
            'text' => 'label 1',
            'text_safe' => 'label_1',
            'type' => 'default'
        ),
        1 => array(
            'text' => 'label 2',
            'text_safe' => 'label_2',
            'type' => 'warning'
        ),
        2 => array(
            'text' => 'label 3',
            'text_safe' => 'label_3',
            'type' => 'danger'
        ),
        3 => array(
            'text' => 'label 4',
            'val' => 'label_4',
            'type' => 'success'
        ),
        4 => array(
            'text' => 'label 5',
            'text_safe' => 'label_5',
            'type' => 'primary'
        )
    );

    echo json_encode($labels);
}
  • 写回答

1条回答 默认 最新

  • 七度&光 2017-10-28 17:54
    关注

    Try with it :

        $.ajax({
        type: 'GET',
        url: SITE_URL + 'nsys/dashboard/noteLabels',
        dataType: 'json',
        success: function (result) {
           var resource = JSON.parse(result);
           var res = resource.type // example if i want to get type
           //Then loop your data, because your data is array now. You can see with it: $('#result').append(res);
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?