![](/img/trans.png)
[英]How to get multiple return value from php by javaScript / jquery?
[英]How to get json return value from php by javaScript / jquery?
這是我的 javaScript 代碼:
$(document).ready(function() {
$('#IMDB').click(function() {
var MovieID = $('#MovieID').val();
$.post('action/action.php', { url: "http://api.themoviedb.org/3/movie/"+MovieID
+"?append_to_response=credits,images&api_key=myapikey" }, function(data) {
var aliases = data.aliases;
document.getElementById('GamesTitle').value = aliases;
});
});
});
這是我的 php 代碼
header('Content-Type: application/json');
$url = $_POST['url'];
$context = stream_context_create(['http' => ['user_agent' => 'API Test UA']]);
$response = file_get_contents($url, false, $context);
$json = json_decode($response, true);
foreach($json as $item){
if($item['aliases'] !== null && $item['aliases'] !== 'O' && $item['aliases'] !== '1'){
$aliases .= $item['aliases']; // its give the result fine in php page
}
}
echo json_encode(array(
'aliases' => $aliases
));
return true;
但是當我點擊我的按鈕獲取所有信息時,它沒有得到任何信息。 我在控制台或任何地方都沒有收到任何錯誤,所以我找不到我的問題。
當我在 php 頁面中返回值時,它會像這樣返回 - >
O null null null null {我的結果} 1
這就是為什么我必須跳過這個 O 和 1
if($item['aliases'] !== null && $item['aliases'] !== 'O' && $item['aliases'] !== '1') {// .... now its ok }
如果您從 php 中刪除它,它將正常工作。 像這樣
header('Content-Type: application/json');
$url = $_POST['url'];
echo json_encode(array(
'aliases' => 'test' /// now its work fine
));
return true;
添加."json"
... 像:
$(document).ready(function() {
$('#IMDB').click(function() {
var MovieID = $('#MovieID').val();
$.post('action/action.php', { url: "http://api.themoviedb.org/3/movie/"+MovieID+"?append_to_response=credits,images&api_key=myapikey" }, function(data) {
var aliases = data.aliases;
document.getElementById('GamesTitle').value = aliases;
}, "json");
});
});
現在返回data
將被解釋為 JSON。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.