gpt4 book ai didi

php - Ajax 和 php 响应不起作用

转载 作者:行者123 更新时间:2023-11-30 23:48:58 25 4
gpt4 key购买 nike

我真的很喜欢这里的帮助,通常我只是通过阅读现有的帖子来解决到目前为止的任何问题。但是这次我无法安静地弄清楚我的代码有什么问题。

我想在表单中写入特定 Tablerow 的 dataildata 值。数据存储在 mysql 数据库中,我使用 php 访问它。

问题是 Ajax 请求似乎不起作用。成功或错误事件都不会被触发。尽管 alter(id) 工作得很好。

这是函数的 Ajax 调用:

$( "#table1 tbody tr" ).on( "click", function() {
var id = $(this).attr('id');
alert( id );
$.ajax({
type: 'POST',
url: 'getDetailData.php',
dataType: 'json',
data: {id : id },
success: function(data){
$("#inputWstId").val(data.WST-ID);
$("#inputSerialNumber").val(data.SERNO);
$("#inputName").val(data.NAME);
alert(data.NAME);
alert ("Test");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
});

这是 .php 文件:

<?php
header('Content-Type: application/json');
$connect = include 'connect.php';
if (isset ($_POST['id'])){
$id= $_POST['id'];
}
$query = " select * FROM pci WHERE id= ". $id ;
$result = mysql_query($query);
$ligne = mysql_fetch_array($result);
$data = array(
"WST-ID" => $ligne["WST_ID"],
"SERNO" => $ligne["SERNO"],
"NAME" => $ligne["NAME"]
);
mysql_close($connect);
echo (json_encode($data));
?>

如果您需要更多源代码或其他任何东西,请告诉我 - 非常感谢您的帮助!!

WST_ID 而不是 WST-ID 确实有效 - 非常感谢!为什么,我不能在我的值(value)观中使用“-”……我想还有很多东西要学;)

最佳答案

 In the PHP part you need add two more headers :

header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: GET, POST');

Add the two line of coding with the:

header('Content-Type: application/json');

change the:

echo (json_encode($data));

to:

print $my_json = json_encode($data);

or:

print_r ($my_json);



Hope it will work.

关于php - Ajax 和 php 响应不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20968095/

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