Laravel - 通过路由传递参数后Javascript不起作用
Posted
技术标签:
【中文标题】Laravel - 通过路由传递参数后Javascript不起作用【英文标题】:Laravel - Javascript doesn't work after I passed a parameter through route 【发布时间】:2021-11-17 10:09:10 【问题描述】:我正在尝试创建一个网站,展示来自各种电子市场的所有搜索结果。我正在使用 javascript 来显示结果,并且在我尝试通过路由传递参数之前它工作正常。但是一旦我这样做了,结果就停止了。我认为这与Javascript有关。代码如下:
<?php
use Sunra\PhpSimple\htmlDomParser;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.tokopedia.com/search?st=product&q=hp%2024mh');
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_TIMEOUT, 300);
$response = curl_exec($ch);
curl_close($ch);
file_put_contents("text.html", $response);
echo $keyword;
// $url = 'https://www.tokopedia.com/search?st=product&q=24g2';
// $content = file_get_contents($url);
// $first_step = explode('<div class="css-1d1aa4" data-testid="imgSRPProdMain">' , $content );
// $second_step = explode("</div>" , $first_step[1] );
// echo $second_step[0];
?>
@extends('layout.app')
@section('title')
<title>안녕</title>
@endsection
<!-- Custom CSS -->
<link href="/css/card.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
@section('body')
<div class="container-fluid bg-trasparent my-4 p-3" style="position: relative;">
<div class="row py-5">
<div class="col-sm d-flex justify-content-center">
<h3>Search Result</h3>
</div>
</div>
<div class="row row-cols-1 row-cols-xs-2 row-cols-sm-2 row-cols-lg-4 g-3" id="carda">
</div>
</div>
@endsection
<script>
$(document).ready(function()
var img;
$( "#carda" ).load( "text.html .css-1d1aa4 > img", function()
var ancestor = document.getElementById('carda');
var child = ancestor.getElementsByTagName('*');
var i;
for (i = 0; i < 3; i++)
child[0].remove();
$( " #carda > img " ).removeClass("success fade").addClass("productImage");
$( " .productImage ").wrap("<div class='col'><div class='card h-100 shadow-sm'></div></div>");
$( " .card ").append("<div class='card-body'><div class='clearfix mb-3'> <span class='float-start badge rounded-pill bg-success'>Rp. 1,000,000.00</span></div><h5 class='card-title'>Lorem, ipsum dolor sit amet consectetur adipisicing elit. Veniam quidem eaque ut eveniet aut quis rerum. Asperiores accusamus harum ducimus velit odit ut. Saepe, iste optio laudantium sed aliquam sequi.</h5><div class='text-center my-4'> <a href='#' class='btn btn-warning'>View Product</a> </div></div>");
);
);
</script>
这是 web.php 的代码:
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SearchController;
Route::get('/', function ()
return view('main');
);
Route::post('/search', [SearchController::class, 'search'])->name('search');
Route::get('/search/keyword', [SearchController::class, 'index'])->name('result');
Route::get('/result', function()
return view('result');
);
Route::get('/test', [SearchController::class, 'test'])->name('test');
?>
还有 SearchController 代码:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class SearchController extends Controller
public function index($a)
return view('result', [
'keyword' => $a
]);
public function search(Request $request)
return redirect()->route('result', [$request->keyword]);
public function test()
return view('result');
?>
谁能告诉我这有什么问题?任何帮助,将不胜感激!谢谢:D
【问题讨论】:
【参考方案1】:我不知道怎么做,也不知道为什么,但是当我通过路由传递参数时,“text.html”的路径不起作用。我做了一些测试,发现它需要的路径是“yourweb.com/path here”而不是目录。我通过在 web.php 中为 text.html 添加新路由解决了这个问题。
【讨论】:
【参考方案2】:如果该数组不是关联的,则无需在数组中传递单个参数。
改为这样做:
return redirect()->route('result', $request->keyword);
【讨论】:
似乎不是解决方案,因为它仍然不会显示结果,但感谢您提供信息! :D以上是关于Laravel - 通过路由传递参数后Javascript不起作用的主要内容,如果未能解决你的问题,请参考以下文章