weixin_33730836 2014-09-17 12:50 采纳率: 0%
浏览 3

JSON响应Laravel 4

I'm am very new to using JQuery and Ajax so what I am struggling with may be simple.

I have a laravel method that returns a JSON response. I am calling the method in AJAX which returns the data correctly as shown below

{"contribution_id":"72","name":"October 2133","at_total":"12","at_key_accounts":"12","at_targeted_accounts":"12","at_sundry_accounts":"12","at_new_business":"12","yd_total":"12","yd_key_accounts":"12","yd_targeted_accounts":"12","yd_sundry_accounts":"12","yd_new_business":"12","created_at":"2014-09-16 15:17:05","updated_at":"2014-09-16 15:17:05"}

In my view I am trying access each part of the data individually. I can alert all of the JSON but I am unable to just return one of the variables.

This is my AJAX code, it alerts "undefined"

<script>
var con_id = $("#con_id").val()
$.ajax({
  url: "/tpquarterly/laravel/public/getContribution/" + con_id,

  context: document.body
}).done(function(data2) {

    alert(data2.contribution_id)
  $( this ).addClass( "ere" );
});
  • 写回答

1条回答 默认 最新

  • weixin_33697898 2014-09-17 13:04
    关注

    you need to specify dataType option in your ajax call like below

    var con_id = $("#con_id").val()
    $.ajax({
       url: "/tpquarterly/laravel/public/getContribution/" + con_id,
       dataType: "json",
       method: "GET",
    }).done(function(data2) {
      alert(data2.contribution_id)
      $( this ).addClass( "ere" );
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败