Support the ongoing development of →
Last updated 3 months ago.

So... After a while having errors I finish a ajax zipcode query using post..

It's simple but can help you

First, my Controller (EnderecoController.php)

class EnderecoController extends Controller {
    public function buscacep() {
        $cep = Request::input('cep');
        $token = '######################';
        $url = '' . $cep;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Token token="' . $token . '"'));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        return response(curl_exec($ch));

In my case the return is already in json so I took the response()->json(curl_exec($ch));

You have to pay attention with your 'use' files.

//use Illuminate\Http\Request;
use Request;
use app\Http\Controllers\Controller;

I could not use request as a parameter and can't explain why... (don't forget to change the app name hehe)

Second, my route.php. Nothing new.

Route::post('cep', '[email protected]');

If you do a function in route using Request don't forget the "use Request"

use Request;
Route::post('cep', function(){
   return response()->json(Request::input('cep'))

And finally my ajax script

    url: 'cep',
    type: 'post',
    data: {_token: $('input[name=_token]').val(), cep: $('input[name=cep]').val()},
    dataType: 'JSON',
    success: function (data) {

Obviously you can use the url helper functions, like url: "{{action('[email protected]')}}".

I think this is all i have.

Last updated 6 years ago.

@alexisdeza Thank you for your example – it was very helpful.

Now, my ajax request works, but I have a little strange problem yet. First my files:

// routes.php

Route::post('/msg/add', '[email protected]');

// MessageController.php

class MessageController extends Controller
    public function add()
        if (Auth::check()) {
            $msg = new Message;
            $msg->content = Request::input('message');
            $msg->user_id = $Auth::user()->getId();

// script.js

    type: 'POST',
    url: '/msg/add',
    dataType: 'JSON',
    data: {
         _token: $('meta[name="csrf-token"]').attr('content'),
        message: $('#message').val()
    success: console.log('Message ' + message + ' added')

It works great – my messages are saved in a database, but I still have an error in browser console:

Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Any idea what is wrong?

Last updated 6 years ago.

I usually find 500 errors changing the route method to get, passing parameters in url and using the laravel debug, probably some function is behaving strangely. See if it is not related to the return too, put something like return response()->json("success") in your controller function.

Edit: In your function you use $Auth, this dont work for me..

Last updated 6 years ago.

Sign in to participate in this thread!


Your banner here too?

lukasz lukasz lukasz Joined 15 Feb 2016


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2022 - All rights reserved.