也许这似乎很容易,但我找不到任何有用的解决办法。假设我有一个网址,其值为:
/?userid=1
如何使用AJAX传递1的值?此外,userid也会根据已按下的链接进行更改,因此不能用AJAX静态地发送。
$.ajax({
type: "POST",
url: "actions.php?action=getUserProfileData",
data: "userid=" + userid, // I am talking about this part发布于 2018-04-04 16:17:34
GET不支持发送像POST或PUT这样的数据,所以您需要像您已经在做的那样将URL中的所有内容连接起来:
$.ajax({
type: "GET",
url: "actions.php?action=getUserProfileData&userid="+ userid
});发布于 2018-04-04 16:21:47
(试试看:)
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("button").click(function() {
$.ajax({
url: "https://jsonplaceholder.typicode.com/comments",
type: "get",
data: {
postId: 1
},
success: function(response) {
console.log(response);
},
error: function(xhr) {
console.log(error);
}
});
});
});
</script>
</head>
<body>
<button>Get Data</button>
</body>
</html>发布于 2018-04-04 16:26:02
我想功能能帮你解决问题。
function postAction(userid) {
$.ajax({
type: "POST",
url: "actions.php?action=getUserProfileData",
data: "userid=" + userid,
success: function(data) {
alert(data);
}
});
}
function getUser(id) {
$.ajax({
type: "GET",
url: "URL.php?userid=" + id,
success: function(data) {
postAction(data.id)
}
});
}
getUser("1");当您需要变量时,函数要确保变量在作用域中(基本上是闭包)。
https://stackoverflow.com/questions/49655771
复制相似问题