JSON response laravel 4


JSON response laravel 4

我对使用JQuery和Ajax非常陌生,所以我所要解决的问题可能很简单。

我有一个laravel方法,它返回一个JSON响应。我正在AJAX中调用方法,该方法正确返回数据,如下所示

{"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"}

在我看来,我正在尝试单独访问数据的每一部分。我可以提醒所有的JSON,但我不能只返回其中一个变量。

这是我的AJAX代码,它提醒"未定义"的

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

您需要在ajax调用中指定dataType选项,如下所示

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