Successful ajax script doesn't give me response

时间:2015-10-06 08:14:12

标签: javascript php jquery ajax laravel

The general problem is that I cannot get back echo or return through ajax from another file.The alert(msg) is empty. Does that prevent.default stop sending the GET? I am not very fluent in programming, could you please help me in this?

I have my simple form:

<form class='findAndBlock1' method="GET" action="">
    <input type='text' name="nameToBlock1" placeholder="    who do you want to block?" class='nameInput'>
    <input type='submit' value="Search" class='submitInput1'>
</form>

After clicking it, the ajax script starts:

<script>
$(".submitInput1").click(function(){

    event.preventDefault();

    $.ajax({
        type: "GET",
        url: "/searchFriendsToBlock",
        data: {

        },
        success : function(msg) {
            alert(msg);
        },
        error : function(error) {
            alert('error');
        }
    });   
});
</script>

It is directed to the script that is routed like this:

Route::any('/searchFriendsToBlock', 'SettingsController@searchFriendsToBlock');

Here is the script that is run through ajax:

   public function searchFriendsToBlock() {

        $q = Input::get('nameToBlock');
        if (strlen($q) < 3)
            return null;

        $users = DB::table('users')->where //here goes some long request

        foreach ($users as $user) {

            if (!$user->f_first_name_public)
                $user->first_name = "";
            if (!$user->f_last_name_public)
                $user->last_name = "";

            $user->avatar = User::getUserAvatar($user->id);
            $user->id = "";
            $user->type = "user";
            $newArr[] = $user;
        }
        echo "hello";
        return Response::json($newArr);
    }

1 个答案:

答案 0 :(得分:1)

Use dataType parameter in ajax request as you are sending response in json format the default dataType is set to html in jQuery.ajax()

<script>
    $(".submitInput1").click(function(){

        event.preventDefault();

        $.ajax({
            type: "GET",
            dataType: "json",
            url: "/searchFriendsToBlock",
            data: {

            },
            success : function(msg) {
                alert(msg.type);
            },
            error : function(error) {
                alert('error');
            }
        });

    });
</script>

And Your script should be like this

public function searchFriendsToBlock() 
{                                  
    $q = Input::get('nameToBlock'); 
    if (strlen($q) < 3)
        return null;

    $users = DB::table('users')->where //here goes some long request
    $response  = array();
    foreach ($users as $user) {
        if (!$user->f_first_name_public)
            $user->first_name = "";
        if (!$user->f_last_name_public)
            $user->last_name = "";

        $user->avatar = User::getUserAvatar($user->id);
        $user->id = "";
        $user->type = "user";
        $newArr[] = $user;
    }
    $response['type'] = 'sussess';    
    $response['data'] = $newArr;    
    return Response::json($response);
}