Support the ongoing development of Laravel.io →
posted 8 years ago
Requests
Last updated 2 years ago.
0

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 = 'http://www.cepaberto.com/api/v2/ceps.json?cep=' . $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', 'EnderecoController@buscacep');

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

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

Obviously you can use the url helper functions, like url: "{{action('EnderecoController@buscacep')}}".

I think this is all i have.

Last updated 8 years ago.
0

@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', 'MessageController@add');

// 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();
            $msg->save;
        }	
    }
}

// script.js

$.ajax({
    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)    http://laravel.dev/msg/add

Any idea what is wrong?

Last updated 8 years ago.
0

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 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

lukasz lukasz Joined 15 Feb 2016

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.