我使用id从api获取一些信息,然后让用户添加一些额外的信息,然后将它们作为json发送到php。但在php中,json_decode返回null。
这是我的jquery: -
$('#btn').click(function(){
var imdbid=$('#tst').val();
var url = "https://www.omdbapi.com/?i="+imdbid+"&plot=full&r=json"
$.ajax({
url:url,
dataType:'json',
success:function (json) {
var data=json
var foo=$('form').serializeJSON();
var merged=$.extend(data,foo);
$.ajax({
url:"process.php",
type:"POST",
data:merged,
success:function(data){
console.log(data);
}
})
}
})
})
And this is my php
if(isset($_POST['imadbID']) || isset($_POST['comment'])|| isset($_POST['Title']) || isset($_POST['Year'])
|| isset($_POST['released']) || isset($_POST['Runtime'])|| isset($_POST['Actors'])||isset($_POST['Country']) || isset($_POST['Language'])
|| isset($_POST['Plot']) || isset($_POST['Poster']) || isset($_POST['Director']) || isset($_POST['Writer']) || isset($_POST['imdbRating']) || isset($_POST['tst'])){
$imdbid=json_decode($_POST['imdbID']); $comment=json_decode($_POST['comment']);
$title=json_decode($_POST['Title']); $year=json_decode($_POST['Year']); $released=json_decode($_POST['Released']);
$runtime=json_decode($_POST['Runtime']); $actor=json_decode($_POST['Actors']); $country=json_decode($_POST['Country']);
$lang=json_decode($_POST['Language']); $plot=json_decode($_POST['Plot']); $poster=json_decode($_POST['Poster']);
$director=json_decode($_POST['Director']); $writer=json_decode($_POST['Writer']); $rating=json_decode($_POST['imdbRating']);
$test=json_decode($_POST['tst']);
}
else {
echo"error";
}
try {
var_dump($title);var_dump($runtime);var_dump($plot);var_dump($released);var_dump($poster);var_dump($lang);var_dump($imdbid);var_dump($country);var_dump($rating);
$sql=$conn->prepare("INSERT INTO films (title,length,description,releasedate,bannerpath,language,imdbid,country,rating)
VALUES (:t,:r,:p,:re,:po,:s,:i,:c,:ra)");
$sql->execute(array(":t"=>$title, ":r"=>$runtime, ":p"=>$plot, ":re"=>$released, ":po"=>$poster,":s"=>$lang,":i"=>$imdbid, ":c"=>$country, ":ra"=>$rating));
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
我已经尝试了var_dump(),只有":ra"=>$rating
不是Null。但其他一切都是Null.Been寻找解决方案,但似乎找不到。
当我console.log
合并时,显示
object {Title: "Mother's Day", Year: "2016", Rated: "PG-13", Released: "29 Apr 2016", Runtime: "118 min"…}