0
PHP 웹 서비스를 만들었습니다. Jquery를 사용하여 JSON 결과를 구문 분석하려고하지만 항상이 오류가 발생합니다. undefined. 여기
은 JSON 결과입니다 :JSON 파일을 구문 분석 할 때 정의되지 않음
{"posts":[{"post":{"user_id":"1","user_fullname":"taieb baccouch","user_email":"[email protected]","user_password":"toto123","user_status":"1"}},{"post":{"user_id":"2","user_fullname":"zahra dagrir","user_email":"[email protected]","user_password":"zahra123","user_status":"1"}}]}
그리고 내 jQuery 코드 :
내 웹 service.php 코드 여기<!DOCTYPE html>
<html>
<head>
<title>Web service</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function() {
$("button").click(function() {
$.getJSON("http://localhost/WebService/web-service.php?format=json", function(posts) {
$.each(posts, function(i, field) {
$("div").append(field.user_fullname + " ");
});
});
});
});
</script>
</head>
<body>
<button>Get JSON data</button>
<div></div>
</body>
</html>
입니다 :
<?php
$format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
/* connect to the db */
$link = mysql_connect('localhost', 'root', 'cabiat705') or die('Cannot connect to the DB');
mysql_select_db('webservice', $link) or die('Cannot select the DB');
/* grab the posts from the db */
$query = "SELECT * FROM users ";
$result = mysql_query($query, $link) or die('Error query: ' . $query);
/* create one master array of the records */
$posts = array();
if (mysql_num_rows($result)) {
while ($post = mysql_fetch_assoc($result)) {
$posts[] = array('post' => $post);
}
}
/* output in necessary format */
if ($format == 'json') {
header('Content-type: application/json');
echo json_encode(array('posts' => $posts));
} else {
header('Content-type: text/xml');
echo '<posts>';
foreach ($posts as $index => $post) {
if (is_array($post)) {
foreach ($post as $key => $value) {
echo '<', $key, '>';
if (is_array($value)) {
foreach ($value as $tag => $val) {
echo '<', $tag, '>', htmlentities($val), '</', $tag, '>';
}
}
echo '</', $key, '>';
}
}
}
echo '</posts>';
}
/* disconnect from the db */
mysql_close($link);
내가 그것을 어떻게 해결할 수 있습니까?
있습니까? –
@ A.Wolff 내 코드를 업데이트했습니다. 이것을 봐주세요 –