0

I am trying to send data from angular js to server.. Laravel5 is running on my server.. I can fetch data using this but can not send data.. My code is bellow

AngularJs Code:

var request = $http({
        method: "get",
        url: "http://www.myserver.com/json",
        data: { email: $scope.email },
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        })
        request.success(function (data) {
            alert(data.email);
        });

Server code :

route is Route::get('json','JsonController@Json_login');

Controller code is

namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Input;     

class JsonController extends Controller
{

    public function Json_login()
    {   

        $email = Input::get('email');
        $arr_Id_Status=array('email'=>$email);  
        echo json_encode($arr_Id_Status);
        }
}

But it giving me null alert

2 Answers 2

0

Sounds like you should be using a POST rather than a GET request. You would need to create an appropriate POST route on your server but it's pretty much the same syntax on the client side, just change GET to POST.

var request = $http({
        method: "POST",
        url: "http://www.saadetera.com/json",
        data: { email: $scope.email },
        headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
    }).success(function (data) {
        alert(data.email);
    });

Not used Laravel for a while but it would be something like:

Route::post('json','JsonController@handleLoginRequest');
Sign up to request clarification or add additional context in comments.

1 Comment

I think that means you've not set the route up correctly. You need a Route::post that points to a method that you have defined on your controller. Check that your routes point to the correct methods.
0

Ideally, sending data will require a POST method while fetching will be using a GET method. Let's make the complete flow assuming you want to call a controller called your_controller and use its your_method function :

This suits for Laravel 5

Laravel Controller :

use Illuminate\Http\Request;

public function your_method(Request $request)
{
    $data = $request->input(my_data);
    //Server side data processing here

    //Returning Json or anything else here
    return response()->json(your_answer_here);
}

Route :

Route::post('your_url', ['uses' => 'your_controller@your_method']);

AngularJS : Here is the chunk of code allowing data sending, it requires $http service. Let's assume your data is contained in the my_data object.

$http.post('your_url', my_data)
.then(you_callback_success, your_callback_fail);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.