gpt4 book ai didi

php - 将 JSON 数组返回给 ajax

转载 作者:行者123 更新时间:2023-12-01 07:54:53 24 4
gpt4 key购买 nike

我被困在这里了。

我试图在选择 div 时触发 ajax,然后获取 JSON 响应。我所能返回的只是错误代码,我不确定为什么。这是简化版本(仍然得到与实际相同的响应)。也许有更好的方法?感谢您的帮助。

jquery:

<html lang="en">
<meta charset="utf-8">
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
<style>
#selectable .ui-selecting { background: #FECA40; }
#selectable .ui-selected { background: #F39814; color: white; }
#selectable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
#selectable li { margin: 3px; padding: 0.4em; font-size: 1.4em; height: 18px; }
</style>

<script>

$(function(){


$("#selectable").selectable({
selected : function(event, ui) {
if($(ui.selected).hasClass('ui-widget-content')){
var rptID = ($(ui.selected).val());
$.ajax(
{

type: "POST",
dataType: "json",
url: "hello2.php",
success: function( response )
{

alert(1);
},
error: function( error )
{
console.log(error);


}

} );




}
}
});

});
</script>

<body><ol id="selectable">
<li class="ui-widget-content" value=1 >Item 1</li>
<li class="ui-widget-content" value=2>Item 2</li>
<li class="ui-widget-content" value=3>Item 3</li>
</ol></body>
</html>

php:

<?php
$json = array('hello' => 'hello');
header('Content-Type: application/json');
echo json_encode($json);
?>

最佳答案

php?>将在 PHP 级别导致语法错误/警告,并且该语法错误输出可能会被标记到 JSON 输出的末尾,从而在客户端上导致 JSON 解析错误。

PHP 代码块很简单 <?php ... ?>

例如

[marc ~]$ cat z.php
<?php
echo json_encode('foo');
php?>
[marc ~]$ php z.php
"foo"PHP Notice: Use of undefined constant php - assumed 'php' in /home/marc/z.php on line 3

编辑---现在我看到OP已经编辑掉了php?>东西...

关于php - 将 JSON 数组返回给 ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25387275/

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