json无法使用代码

时间:2016-07-22 06:15:10

标签: javascript php ajax

我有一些代码如何使用json链接php。



<?php

$con = mysqli_connect('localhost', 'ganesh', '5565','ganesh');
$sql = "INSERT INTO `equation`VALUES('','123','123')";
$result = mysqli_query($con, $sql);

 echo ' Updated successfully';

?>
&#13;
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>

</head>
<body>
<script>

		$(document).ready(function(){
    $("button").click(function(){
        $.getJSON("insert12.php", function(result){
            $.each(result, function(result){
                $("#file").html(result);
            });
        });
    });
});
		
</script>

<input type="text" id="input"placeholder="Search..." >
<button>solve</button>

<div id="result">
  <ul></ul>
</div>
<ul id="file">
  
  
</ul>


</body>
</html>
&#13;
&#13;
&#13;

json不工作如何获取输入数据请帮帮我。例如输入(某些文本)输出(某些文本)是使用json打印的

2 个答案:

答案 0 :(得分:0)

在PHP代码中,您必须以JSON格式提供响应

<?php

$con = mysqli_connect('localhost', 'ganesh', '5565','ganesh');
$sql = "INSERT INTO `equation`VALUES('','123','123')";
$result = mysqli_query($con, $sql);

$response=array("type"=>"success","message"=>"Updated successfully");
ob_clean();
echo json_encode($response);

答案 1 :(得分:0)

尝试对结果进行编码,使用json_encode($response)将数组转换为JSON格式,如@Haresh所述,并添加header('Content-Type: application/json');以避免使用echo

<?php
header('Content-Type: application/json');
$con = mysqli_connect('localhost', 'ganesh', '5565','ganesh');
$sql = "INSERT INTO `equation`VALUES('','123','123')";
$result = mysqli_query($con, $sql);

json_encode(array("type"=>"success","message"=>"Updated successfully"));

?>