gpt4 book ai didi

javascript - 将 JSON 对象发送到 Web 服务时 $.ajax 函数失败

转载 作者:行者123 更新时间:2023-12-02 17:01:09 25 4
gpt4 key购买 nike

我创建了一个示例 Web 服务来存储和检索数据。 PHP Web 服务有 2 个脚本,分别称为 getData.php 和 saveData.php,getData 返回 json 响应,saveData 将 json 对象保存到数据库。

getData.php

<?php
require_once ('../database.php');
mysqli_select_db($conn, $database);

$query = "SELECT * FROM user ORDER BY id ASC";

$result = mysqli_query($conn, $query) or die(mysqli_error($conn));

$rows = array();

while($packages = mysqli_fetch_assoc($result)) {
array_push($rows, $packages);
}

header('Content-type: application/json');
echo json_encode($rows);
?>

saveData.php

<?php

require_once ('../database.php');
mysqli_select_db($conn, $database);

if (isset($_POST['json'])) {
$jsonObj = $_POST['json'];

$jsonObj = json_decode($jsonObj);

$query = "INSERT INTO user (first_name, last_name, description)"
. " VALUES ('".$jsonObj->{'first_name'}."', '".$jsonObj->{'last_name'}."', '".$jsonObj->{'description'}."')";

mysqli_query($conn, $query);

header('Content-type: application/json');
echo json_encode($_POST['json']);
}
?>

它位于我的 wamp/www 文件夹中名为 testService 的文件夹中。然后我有另一个名为 testConsume 的文件夹,其中包含带有简单表单的 html 页面,该表单将数据发送到 testService/saveData.php 文件。

HTML

<form role="form">
<div class="form-group">
<input name="first_name" id="txtFirstName" class="form-control" placeholder="First Name" type="text" />
</div>
<div class="form-group">
<input name="last_name" id="txtLastName" class="form-control" placeholder="Last Name" type="text" />
</div>
<div class="form-group">
<input name="description" id="txtDescription" class="form-control" placeholder="Description" type="text" />
</div>
<a id="submit" class="btn btn-success" onclick="sendData()">Submit</a>
</form>

在脚本中,sendData() 函数获取数据并将其作为 json 对象发送

function sendData() {
var firstName = $('#txtFirstName').val();
var lastName = $('#txtLastName').val();
var description = $('#txtDescription').val();

var jqxhr = $.ajax({
url: 'http://localhost:8080/testService/json/saveData.php',
type: 'POST',
contentType: 'application/json',
data: { json: JSON.stringify({
first_name: firstName,
last_name: lastName,
description: description
})},
dataType: 'json'
});

jqxhr.done(function() {
alert("Success! " + firstName + " " + lastName + " is a " + description);
});
jqxhr.fail(function() {
alert("Failed");
});

}

当我运行 testConsume/index.html 并单击“提交”时,会显示警报消息“失败”。当我检查数据库时,没有添加任何数据。我做错了什么?

最佳答案

删除 contentType: 'application/json'

您正在发送嵌入 application/x-www-form-urlencoded 数据中的 JSON,而不是纯 JSON。

<小时/>

或者。发送并解析实际的纯 JSON:

contentType: 'application/json',
data: JSON.stringify({
first_name: firstName,
last_name: lastName,
description: description
}),

在你的 PHP 中:

if (stripos($_SERVER["HTTP_CONTENT_TYPE"], "application/json")===0) {
$jsonObj = json_decode(file_get_contents("php://input"));
}

关于javascript - 将 JSON 对象发送到 Web 服务时 $.ajax 函数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25708454/

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