简体   繁体   中英

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM