1

I can't through the $request->ajax(), I can get and dd($data), but it always return false, what's wrong with my code.could anyone help me? thanks.

route

Route::post('test', 'BlogController@test');

view

<form method="POST" id="form-ajax" action="/test">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <div class="form-group">
        <label for="name1">Name1</label>
        <input type="text" class="form-control" name="name1" id="name1">
    </div>
    <div class="form-group">
        <button type="submit" class="btn btn-primary" id="test_btn">
            Submit
        </button>
    </div>
</form>

controller

public function test(Request $request)
{
    $data = $request->input('name1');
    if ($request->ajax()) {
        $response = array(
            'name' => $data,
            'status' => 'success',
        );
        return response()->json($response);
    } else
        return response()->json(['msg' => 'false']);
}

js

$(document).ready(function() {
            $('#test_btn').click(function() {
                $.ajax({
                    url: '/test',
                    type: 'post',
                    data: {'_token': $('input[name=_token]').val(), 'name1': $('input[name=name1]').val()},
                    success: function(data) {
                        console.log(data);
                    },
                    headers: {'X-Requested-With': 'XMLHttpRequest'},
                    dataType: 'json'
                });
            });
        });
汤小浪
  • 33
  • 7

1 Answers1

0

If you dig in the code, you will find out that $request->ajax() method relies on this logic (see vendor/symfony/http-foundation/Request.php):

$this->headers->get('X-Requested-With');

jQuery does set this header by default. First action you should do – see how your AJAX call looks in the Developer Tools in your browser. You can see full HTTP headers there – do the headers contain this X-Requested-With line?

If it's not there (unlikely but who knows), you could manually add it to your AJAX data object, eg: "X-Requested-With":"XMLHttpRequest".

Also, I would advise to replace $data = $request->get('name1'); with

$data = $request->input('name1');

Which is more correct way to get an input variable (get() method doesn't even exist in the documentation).

Denis Mysenko
  • 6,366
  • 1
  • 24
  • 33
  • thanks for your answer . and I added what you said. after that ,unfortunately,the headers also not contain the X-Requested-With – 汤小浪 Feb 28 '16 at 05:43
  • Try this method: http://stackoverflow.com/questions/8163703/cross-domain-ajax-doesnt-send-x-requested-with-header – Denis Mysenko Feb 28 '16 at 05:49
  • I found my problem, when I click the submit button, it through the form action post data, not through the ajax url, but I don't know how to solve this. – 汤小浪 Feb 28 '16 at 08:57
  • To avoid this, disable default action (which is form submit): $('#test_btn').click(function(e) { e.preventDefault(); ... }. Having said that, my original advice was correct - browsers Developers Tools would have shown you this problem right away :) – Denis Mysenko Feb 28 '16 at 09:11
  • yeah, it works. but I have a doubt, why other‘s code don't need this? – 汤小浪 Feb 28 '16 at 10:20
  • Maybe in other places you don't rely on $request->ajax()? And then regular FORM submit would work fine, you would see no difference – Denis Mysenko Feb 28 '16 at 10:23