【问题标题】:Dynamically inserting HTML into View through AJAX with JS Array in LARAVEL在 LARAVEL 中使用 JS 数组通过 AJAX 将 HTML 动态插入到视图中
【发布时间】:2019-03-19 18:38:40
【问题描述】:

您好,我是 Laravel 的新手,遇到这样的情况:我有一个 HTML 表单并使用 AJAX 以相同的形式加载一些 HTML,例如“partial.blade.php”,这与静态 HTML 完美配合,但是,

我想发送一个带有 AJAX 请求的 JS 对象数组来动态加载 HTML 内容,例如:以相同的形式下拉

能否指导我如何将 JS 数组传递给那段 HTML,以便我可以在“partial.blade.php”中渲染该数组

这是我的代码

这是我想通过这个 AJAX 请求传递的主要形式 HTML 和数组

var dataArray = $('#data1').val();

代码最终包含在 HTML 表单页面中

$.ajax({                
            url: "add-property/residential",
            type: 'GET',
            dataType: 'html',
            data: { dataArr: dataArray },
            contentType: false,
            processData: false,
            success: function(response) {
                console.log(response);
                $('#dynamicForm').html(response);
                //alert(response);
            },
            error: function(response) {
                console.log(response);
                alert('Error....!');
            }
        });

这是我的路线

Route::any('admin/add-property/residential',function() { return view('admin.residential'); });

我只想在这段动态加载的 HTML 中接收那个 JS 数组

【问题讨论】:

  • Js数组转成HTML?!!
  • 我非常确定var dataArray = $('#data1').val(); 必须返回一个字符串。即使那是 php 中的 json 数组,在添加到输入 value 后它仍然是一个字符串。您是否尝试在其上使用 JSON.parse() 将其转换为 JSON?
  • 感谢您的回复,但我没有尝试这种方法
  • 只有你明白我真正想做的事
  • 你能用例子指导我吗

标签: javascript php jquery ajax laravel


【解决方案1】:

当您想将view() 的内容返回给 AJAX 请求时,您可以将视图存储为变量并以 JSON 响应的形式返回:

public function apiResponse(){
  $html = view("admin.residential")->render();

  return response()->json(["html" => $html]);
}

在您的 AJAX 请求中,您可以通过 response.html 访问它:

success: function(response) {
  $("#dynamicForm").html(response.html);
}

要将变量从 AJAX 传递到视图,您可以使用 request 对象。在您的routes/api(或routes/web.php,无论您使用哪个)顶部添加use Illuminate\Http\Request;,然后在您的路由中将其注入函数:

<?php
use Illuminate\Http\Request;
...
Route::any('admin/add-property/residential',function(Request $request){ 
  ...
});

不是$request 可用,您可以通过$request-&gt;input("dataArr"); 访问在AJAX 请求中发送的数据,然后使用-&gt;with(); 将其传递给视图部分:

Route::any('admin/add-property/residential',function(Request $request){ 

  $dataArr = $request->input("dataArr");
  $html = view("admin.residential")->with(["dataArr" => $dataArr])->render();

  return response()->json(["html" => $html]);
});

【讨论】:

  • 非常感谢您的回复,但实际上,当我使用 AJAX 服务器响应未定义变量加载该 HTML 时,我在部分 HTML 中有一些服务器端变量
  • 为了解决这个问题,我想通过 Ajax 发送服务器端数组变量,这样当我加载部分 html 时,我也想再次接收传递的数组变量
  • 你能指导我如何在Route Closure中接收从AJAX发送的数组变量并传递给部分视图
  • 再次感谢您的光临,我刚刚学会了如何将所有视图放在一个变量中以便消失。我刚刚通过从 AJAX 中删除“processData:false”来解决我的问题,现在我正在前进。
【解决方案2】:

根据this

而不是return view('admin.residential');

你需要这样做:return (string) View::make('admin.residential');

编辑

如果你想从 ajax 接收数据,然后加载视图,你可以这样做:

导入Request

use Illuminate\Support\Facades\Response;

将您的死记硬背指向controller 上的特定method

Route::any('admin/add-property/residential',"YourController@returnView");

让您的function 处理数据

public function returnView(Request $request){
    $data = $request->all(); // Here you will have all the data you send via AJ
    //Make sure the $data has all the necessary parameter to load the view
    $view = View::make('my_view', $data);//Here you will pass the data to the view
    return response()->json(["html" => $$view->render()]); // Using @TimLewis return
}

【讨论】:

  • 在没有服务器端变量的情况下渲染部分 html 没有问题
  • @ZeeshanDaDa 我修改了代码,希望能帮到你
猜你喜欢
  • 1970-01-01
  • 2013-05-31
  • 2020-12-05
  • 1970-01-01
  • 1970-01-01
  • 2021-10-11
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
相关资源
最近更新 更多