我正试图从php文件中获取一些json数据。但是程序将(response2)中的数据作为HTML内容获取,而不是json
Uri geturi2 = new Uri(JSON_GetHASSCENE_URL + "&FilmID=" + film.id);
using (HttpClient client2 = new HttpClient())
{
//MultipartFormDataContent content = new MultipartFormDataContent();
HttpResponseMessage responseGet2 = await client2.GetAsync(geturi2);
string response2 = await responseGet2.Content.ReadAsStringAsync();
JObject token2 = JObject.Parse(response2);
List<HasScene> result = JsonConvert.DeserializeObject<List<HasScene>>(response2);
}
问题可能来自php文件。所以,我添加了标题,但问题没有解决。这是php文件
<?php
header('Content-Type: application/json');
//set vars
$filmID = (int)$_GET['FilmID'];
try {
$conn = new PDO('mysql:host=localhost;dbname=********','*****','*********');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
//Create SELECT query
$qry = $conn->prepare("SELECT * FROM Film WHERE FilmID=? ");
$qry->bindParam(1, $filmID);
//Check whether the query was successful or not
if($qry->execute()){
$count = $qry->rowCount();
}
$json_data=array();//create the array
if($count >0) {
foreach($qry as $rec)//foreach loop
{
// all table's columns must be implemented here
//$json_array['id']=$rec['id'];
$json_array['FilmID']=$rec['FilmID'];
$json_array['IsHasScene']=$rec['HasScene'];
//here pushing the values in to an array
array_push($json_data,$json_array);
}
}
echo json_encode($json_data);
?>
(response2)变量的结果看起来像这个
<!DOCTYPE html PUBLIC '"-//W3C//DTD XHTML 1.0 Strict//EN'" '"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'">'n<html xmlns='"http://www.w3.org/1999/xhtml'" xml:lang='"en'" lang='"en'">'n<head profile='"http://gmpg.org/xfn/11'">'n <meta http-equi..."
有什么帮助吗?
感谢
我刚刚发现问题,它在链接中我只是把"&"换成了"?"。并且它在上运行良好