Question by
puntodamar · Jun 23, 2017 at 09:59 AM ·
wwwjsonphp
WWW GET JSON result always null
Hi, im trying to get a json result from a GET method. Here is my php
<?php
include 'include.php';
try{
$stmt = $pdo->prepare('SELECT * FROM mahasiswa');
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_ASSOC);
$result = [
'status' => 200,
'data' => $stmt->fetchAll()
];
echo json_encode($result, true);
}
catch(Exception $e){
$error = [
'status' => $e->getCode(),
'message' => $e->getMessage()
];
echo json_encode($error);
}
?>
and here is my .cs
using UnityEngine;
using System.Collections;
using Boomlagoon.JSON;
using System.Collections.Generic;
public class GETScript : MonoBehaviour{
string URL;
void Start(){
URL = "http://localhost/bm/php/ambilmahasiswa.php";
Dictionary<string,string> headers = new Dictionary<string,string>();
headers.Add ("Content-Type", "application/json");
WWW www = new WWW (URL, null, headers);
// WWW www = new WWW (URL);
StartCoroutine (WaitForRequest(www));
}
IEnumerator WaitForRequest(WWW www){
yield return www;
if(www.error != null){
string serviceData = www.text;
Debug.Log (serviceData.ToString ());
JSONObject json = JSONObject.Parse (serviceData);
if(json == null){
Debug.Log ("No data converted");
}
else{
// ambil data json
Debug.Log (json.ToString ());
}
}
else{
Debug.Log (www.error);
}
}
}
the request is working properly using POSTMAN. thanks.
Comment
Your answer
Follow this Question
Related Questions
Can WWW return a JOSN string? 0 Answers
Can't get echo 1 Answer
Get Internet Year PHP 0 Answers
WWW Error : 404 with working url 1 Answer