【问题标题】:How to print an array coming from Ajax request如何打印来自 Ajax 请求的数组
【发布时间】:2019-11-13 14:20:16
【问题描述】:

这是我的控制器:

public function updateCustomerRecord(Request $request)
    {
        $datda = $request->all(); // This will get all the equest data.
        $teste = "teste";
        $selectt = DB::select('select * from users');
        return response()->json(['success' => true, 'teste' => $teste, 'selectt' => $selectt]);
    }

这是我在视图上的 ajax:

$(document).ready(function(){
    $('#getRequest').click(function(){
        $.ajax({
    method: 'POST', // Type of response and matches what we said in the route
    url: '/customer/ajaxupdate', // This is the url we gave in the route
    data: {'id' : id}, // a JSON object to send back
    success: function(response){ // What to do if we succeed
        var theDiv = document.getElementById("teste");theDiv.innerHTML = response.selectt[1].;
        console.log(response); 
    },
    error: function(jqXHR, textStatus, errorThrown) { // What to do if we fail
        console.log(JSON.stringify(jqXHR));
        console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
    }
    });
    })
})

通过 console.log 我得到以下数据:

我希望做一个 foreach 并回显整个数组。

【问题讨论】:

  • 这里有什么问题?
  • 对不起,我没有关注。您想单独打印整个数组,包括子元素吗?打印 selectt 的内容?
  • 是的,我想单独打印包含子元素的数组。 @Kurisu

标签: php html ajax laravel


【解决方案1】:

如果我是正确的,您想访问 selectt 的内容并单独打印它们,对吗?

使用 foreach 循环很容易做到这一点。

for (const customer of response) {
    console.log(customer);
    // Do stuff with 'customer' if necessary
}

如果不是这样,请随时告诉我,我会相应地更新答案。

【讨论】:

    猜你喜欢
    • 2016-06-28
    • 2020-09-03
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多