Javascript POST方法不显示,但GET方法显示
我的Javascript POST方法不显示,但GET方法显示,javascript,php,ajax,post,Javascript,Php,Ajax,Post,我的POST方法不会显示在网页上,但是我的GET方法会显示,即使我还没有在我的global.js中使用它创建方法。GET方法是否与POST一起提供?我希望我的POST显示notGET。我该怎么做?我知道我的POST是有效的,因为在网络中(即在带有控制台的浏览器中),有POST方法,预览会打印出$\u POST和$\u会话。如何使POST显示在页面上而不是GET Button.php <!doctype html> <html> <head> &
POST
方法不会显示在网页上,但是我的GET
方法会显示,即使我还没有在我的global.js中使用它创建方法。GET
方法是否与POST
一起提供?我希望我的POST
显示notGET
。我该怎么做?我知道我的POST
是有效的,因为在网络中(即在带有控制台的浏览器中),有POST
方法,预览会打印出$\u POST
和$\u会话
。如何使POST
显示在页面上而不是GET
Button.php
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<script src ="global.js"></script>
<title>Button POST</title>
</head>
<body>
<button id ="postbutton">GO</button><br>
</body>
</html>
您的GET方法是有效的,因为您使用了onclick with
location.href
方法。这将始终使用GET方法重定向,以便它可以显示您的输出!但要在ajax中使用POST方法,您需要删除onclick方法并将返回数据附加到body元素
Button.php
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<script src ="global.js"></script>
<title>Button POST</title>
</head>
<body>
<button id ="postbutton">GO</button><br>
</body>
</html>
为什么在同时调用$.ajax()
的按钮中有一个onclick
:
GO
如果要使用$.ajax()
无需单击onclick
。您可以调用location.href
indsidedone(函数(数据){})
如果它满足您的条件,并且您希望将用户重定向到其他地方。您正在设置窗口。location。。。。您不能同时进行Ajax调用和设置位置。为什么要重定向?@epascarello这只是一个例子,我正在做的是将它重定向到另一个不是storage.php
的页面。我认为这不会有什么不同,但看起来确实如此。在Ajax调用完成后,您需要重定向。所以把它放在done方法中。@epascarello以前试过,但没有成功。displatedArray()False
感谢您的回答:D它现在似乎可以工作了,但是如何将它重定向到其他页面并仍然具有该值?我所做的是将该值存储到storage.php
中,并将该值用于另一个页面。
var oject = [{name:'John', age:17},{name:'James', age:22}];
var json = JSON.stringify(oject);
$(document).ready(function(){
$('#postbutton').click(function(){
$('#output').html('sending..');
var jobject = JSON.stringify(oject);
console.log(jobject);
$.ajax({
method:'post',
url:'storage.php',
data:{json:oject},
})
.done(function(data){
console.log(data);
});
});
});
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"> </script>
<script src ="global.js"></script>
<title>Button POST</title>
</head>
<body>
<button id ="postbutton">GO</button><br>
</body>
</html>
var oject = [{name:'John', age:17},{name:'James', age:22}];
var json = JSON.stringify(oject);
$(document).ready(function(){
$('#postbutton').click(function(){
$('#output').html('sending..');
var jobject = JSON.stringify(oject);
console.log(jobject);
$.ajax({
method:'post',
url:'storage.php',
data:{json:oject},
})
.done(function(data){
$("body").append(data);
});
});
});