vendredi 30 juin 2017

Simple ajax search with Laravel return token mismatch

I'm not familiar that much with jquery/ajax and will appreciated some help. I'm trying to make simple ajax search which make curl request and return the result. Everything works fine if I don't use ajax for the search.

This is my controller

function get_curl_content_tx($url) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    $result = curl_exec($curl);
    curl_close($curl);
    return $result;
}

public function getImage(Request $request)
{

$url = get_curl_content_tx('http://ift.tt/2soQLjV'.$request->input('url'));
$items = json_decode($url, true);
$thumb = $items['thumbnail_url'];

    return view('getImage',compact('thumb'));
}

And here is the form in the blade view

    <div class="container">
        <form method="post" action="getImage" role="form">
            <div class="row">
                <div class="col-md-6">
                    <div class="form-group">
                        <input type="text" id="url" name="url" class="form-control" placeholder="Paste URL.">
                    </div>
                </div>
                <div class="col-md-6">
                    <div class="form-group">
                        <button class="btn btn-success" id="submit">Search</button>
                    </div>
                </div>
            </div>
        </form>

        @if(!empty($thumb))       
            <img src="" style="margin-bottom: 15px;">       
        @else                                              
            <p>There are no data.</p>            
        @endif
    </div>

And this is what I've made for ajax and which I think isn't work

$(document).ready(function() {
    $('#submit').on('submit', function (e) {
        e.preventDefault();
        var url = $('#url').val();
        $.ajax({
            type: "POST",
            data: {url: url},
            success: function( msg ) {
                $("#ajaxResponse").append("<div>"+msg+"</div>");
            }
        });
    });
});

Currently when I press search button the error is

TokenMismatchException

I'm using Laravel 5 and I'm not sure how to do it into laravel



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire