gpt4 book ai didi

javascript - 如何使用ajax设置这个值

转载 作者:行者123 更新时间:2023-11-28 08:19:53 25 4
gpt4 key购买 nike

我想知道使用 javascript 执行此 ajax 函数的正确方法是什么。这是代码:

$.ajax({
'url' : '',
'type' : 'POST',
'data' : last_time,
'data' : "last_time=yes",
'beforeSend' : function () {

},

如何设置 2 个数据值?

PHP:

if(isset($_POST['last_time'])){

最佳答案

jQuery 的 $.ajax 方法需要其中之一

  • 查询字符串last_time=yes
  • 或 JSON 对象{last_time: "yes"}

两者都不是。就像这样...

查询字符串:

var dataString = "last_time=yes&date=4162014&action=last_time";

$.ajax({
'url' : 'localhost/actions/last_time.php',
'type' : 'POST',
'data' : dataString,
'beforeSend' : function () {

},

JSON:

var data = {
action: "last_time",
last_time: "yes",
date: "4162014"
};

$.ajax({
'url' : 'localhost/actions/last_time.php',
'type' : 'POST',
'data' : data,
'beforeSend' : function () {

},

使用简单的 php 后端

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST') {
switch ($_POST['action']}) {
case 'last_value':
$return_array = array(
"status" => "great!",
"message" => "Hey there!"
);
die(json_encode($return_array));
break;

default:
$return_array = array(
"status" => "default"
);
die(json_encode($return_array));
break;
}
} else {
die("access denied");
}

关于javascript - 如何使用ajax设置这个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23123956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com