Laravel Auth AJAX没有弹出窗口

Ive been going through this tutorial

Everything works fine from a terminal cURL request but I get a popup for authentication when using jQuery. Is there a way to prevent this popup and pass the login credentials from jQuery to the API? How exactly does --user work from terminal and how do I "simulate" this from jQuery?

Terminal:

$ curl --user username:password http://localhost/api/v1/email
{"error":false,"authEmail":"me@localhost","message":"request success"}

Laravel controller method:

public function index() {
    return Response::json(array(
        'error' => false,
        'authEmail' => Auth::user()->email,
        'message' => 'request success'), 200);
}

Laravel filter:

Route::filter('auth.basic', function() {
return Auth::basic('username');
});

Laravel route:

Route::group(array('prefix' => 'api/v1', 'before' => 'auth.basic'), function() {
Route::resource('email', 'EmailController');
});

jQuery:

(function($) {
$(document).ready(function() {
    $.ajax({
        url: 'http://localhost/api/v1/email',
        type: 'GET',
        dataType: 'json',
        data: {
            username: 'username',
            password: 'password'
        }
    }).complete(function(data) {
        console.log(data.responseJSON);
    });
});
})(jQuery);

You can add a username and a password setting to your jQuery ajax request:

 $.ajax({
        url: 'http://localhost/api/v1/email',
        type: 'GET',
        dataType: 'json',
        username: 'username',
        password: 'password'
    }).complete(function(data) {
        console.log(data.responseJSON);
});

I think that should work.

You can use the beforeSend callback to add the authentication information:

(function($) {

    $(document).ready(function() {

        ajax({

            url: 'http://localhost/api/v1/email',

            type: 'GET',

            dataType: 'json',

            beforeSend: function(xhr) { xhr.setRequestHeader(

                "Authorization",     

                "Basic " + btoa('username' + ":" + 'password')); }

            })

            .complete(function(data) {console.log(data.responseJSON)})

    });

})(jQuery);