[英]How do I do the same in pure JS and PHP, similar to jQuery and Laravel?
当我开始学习 Web 时,经过一点语言学习,我直接进入了库和框架。 现在想学一些原生的JS和PHP。
问题如下:在 jQuery 和 Laravel 上,使用 ajax 请求,我可以同时将数据插入到不同的 ZFC35FDC70D5FC69D5Z 中,例如:
success: function(response) {
$('.basket-prod-name').html(response.name);
$('.modal_order').attr('data-id', response.modalProdId);
$('.basketimg').attr('src', response.img);
$('input[name=inputModal]').val(1);
}
然后,在 controller 中,插入数据如下:
return response()->json([
'modalProdId'=> $product->id,
'name'=> $product->name,
'img'=> "/img/products/".$product->cardImage->path,
]);
但是尝试在纯 JS 和 PHP 中做同样的事情失败了。 例如XMLHttpRequest,我可以使用responseText,然后从PHP发送一个响应(回显“某事”),但是如果我想从PHP中的几个ZFC35FDC70D5FC69D2698883A822C7A5标签发送数据怎么办? SO上有类似的答案,但它有点复杂,没有简单的记录方式吗?
在非常中级的水平上,您可以做一些事情来在“香草”JS 和“无框架”PHP 中实现类似的目标。
您希望 JSON 对您的数据进行编码,然后再将其发送回客户端。
产品.php
<?php
$product->id = "PDX-123";
$product->name = "Product 123";
$jsonResponse = json_encode($product);
echo $jsonResponse;
?>
HTML
<h2>Get data as JSON from a PHP file on the server.</h2>
<p id="demo"></p>
<p id="demo_2"></p>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
product = JSON.parse(this.responseText);
document.getElementById("demo").innerHTML = product.id;
document.getElementById("demo_2").innerHTML = product.name;
}
};
xmlhttp.open("GET", "product.php", true);
xmlhttp.send();
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.