如何使用 jQuery ajax 发送数据
这是我的 html
<!-- <form action="view.php" method="post"> -->
<input type="text" name="text" id="name">
<input type="submit" value="submit">
<!-- </form> -->
我不想使用表单发送数据,因为它只发送命名属性数据,这就是我通过 $.ajax() 方法发送数据的原因 这是脚本,
$(document).ready(function () {
var details = {};
$('input[type="submit"]')
.click(function () {
details = { name: $('input[type="text"]').val() };
$.post({
url: "view.php",
method: "post",
data: details,
success: function (result) {
console.log("done")}
});
window.open('view.php','_self');
});});
毕竟我想检索 view.php 文件上的数据 但我无法在该文件上实现它,它显示数组为空,
<?php
var_dump( $_POST);
//the array is empty
?>
请给我一些建议。
this is my html
<!-- <form action="view.php" method="post"> -->
<input type="text" name="text" id="name">
<input type="submit" value="submit">
<!-- </form> -->
I don't want to send the data by using form Couse it only send named attribute data that's why I am sending data by $.ajax() method
and this is script
$(document).ready(function () {
var details = {};
$('input[type="submit"]')
.click(function () {
details = { name: $('input[type="text"]').val() };
$.post({
url: "view.php",
method: "post",
data: details,
success: function (result) {
console.log("done")}
});
window.open('view.php','_self');
});});
after all this thing i want to retrieve the data on view.php file
but i am fail to achieve it on that file it is showing the array is empty
<?php
var_dump( $_POST);
//the array is empty
?>
suggest me something please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ajax 请求和
window.open
发出的请求是两个不同的请求。可以把它想象成你接到的两个不同的电话……并且对最后的谈话完全没有“记忆”。您需要一个数据库来存储“内存”,但它是别的东西。要将一些数据发送到后端(以对其进行处理)并希望将结果返回到原始页面,请使用
success
回调。原始页面中的 HTML:
原始页面中的 JS:
view.php:
The ajax request and the request made from
window.open
are two different requests. Think of it like two different phone calls that you'd receive... And having absolutely no "memory" of the conversation at the end. You would need a database for that "memory" and it's something else.To send some data to the backend (to process it) and want to have a result back into the original page, use the
success
callback.HTML in original page:
JS in original page:
view.php:
你可以这样做:
You can do like this: