我有問題從我的服務器通過id獲取jsonp。 我相信我的PHP是好的,但我不知道如何解析從PHP服務器的ID到htlm-jquery。如何從php頁面解析id通過jsonp的HTML-jquery頁面
我的PHP頁面:
<?php
header('Content-type: application/json');
include 'config.php';
$con = mysql_connect($dbhost, $dbuser, $dbpass) or die ("Could not connect: " . mysql_error());
mysql_select_db($dbname, $con);
$id=$_GET['id'];
$sql = "select * from ios where id=$id";
$result = mysql_query($sql) or die ("Query error: " . mysql_error());
$records = array();
while($row = mysql_fetch_assoc($result)) {
$records[] = $row;
}
echo $_GET['jsoncallback'] . '(' . json_encode($records) . ');';
////////////////////
?>
而這個偉大的工程,我在PHP ID了JSON對象。 你可以檢查例如http://www.dr-trkuljic.com/tekst.php?id=1(你可以嘗試使用id 2,3和4),你會得到結果。
但我不知道如何解析,在jQuery的在我的html頁面
我在我的html頁面使用此:
$(document).ready(function(){
var output = $('.nesa');
$.ajax({
url: 'http://www.dr-trkuljic.com/tekst.php',
dataType: 'jsonp',
data: 'id',
jsonp: 'jsoncallback',
timeout: 5000,
success: function(data, status){
$.each(data, function(i,item){
//var ispisi = '<li>' + item.naziv + '</li>' ;
var ispisi = item.naziv + item.tekst;
output.append(ispisi);
});
},
error: function(){
output.text('There was an error loading the data.');
}
});
});
所以,我的問題是如何解析(顯示)由我的HTML頁面中的ID? 準確地說,我的服務器http://www.dr-trkuljic.com/tekst.php?id=1的結果如何得到我的html頁面?
謝謝。
謝謝你試圖幫助我。你能不能請示例如何做到這一點,我真的不熟悉jQuery。謝謝 – user3788491
首先,你的php文件中的json顯然有錯誤。我想出了一個解析錯誤,因爲你的json數組被包圍着一組括號:即([{}])。這應該以方括號開始和結束:[]。 – AverageGuy
再次感謝您試圖幫助我。有了這個例子,我得到了跨域請求被阻止,因爲我試圖用我的本地機器連接到我的服務器。我正在使用這個教程:http://samcroft.co.uk/2011/updated-loading-data-in-phonegap-using-jquery-1-5/另外我不能使用id:1,因爲在這樣我會限制查詢只是1.我需要的東西,當我點擊id = 1獲得1,或id = 1000獲得1000等等。你有什麼想法做什麼?謝謝。 – user3788491