Rikudo Pain
Rikudo Pain

Reputation: 461

Single and Multiple JSON response

I stuck here, already try other solution but no hope. I follow example in here, and resulting to this code :

<head>
<script>
    var xmlhttp = new XMLHttpRequest();
    var url = "{{ url('sogi') }}";
    var myArr;
    var out = "";
    var i;
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            myArr = JSON.parse(xmlhttp.responseText);
            for(i = 0; i < myArr.length; i++) {
                out += myArr[i].testing1 + myArr[i].testing2 + '<br>';
            }
            document.getElementById("allit").innerHTML = out;
            alert(out);
        }
    };
    xmlhttp.open("GET", url, true);
    xmlhttp.send();

</script></head>
<body>
<div id="allit"></div>
</body>

Route::get('sogi', 'Sig@testing');

sig controller (case 1) :

public function testing()
    {
        $response = array('testing1' =>'success','testing2' => 'failed');
        return response()->json($response);
    }

sig controller (case 2) :

public function testing($route)
        {
            $check = Jalan::where('a', '=', $route)->toArray();
            return response()->json($check);
        }

But the result always blank even the alert box show blank, how to get JSON response from laravel single array like controller above (case 1) and (case2) if using eloquent?

Upvotes: 0

Views: 376

Answers (1)

Rikudo Pain
Rikudo Pain

Reputation: 461

Solved, got an error in multiple query, it should be like this:

public function testing($route)
{
    $check = Jalan::where('a', 'LIKE', '%'.$route.'%')->toArray();
    return response()->json($check);
}

Upvotes: 0

Related Questions