如何将数据从角度js发送到laravel 5服务器 [英] How to send data from angular js to laravel 5 server
问题描述
我正在尝试将数据从角度js发送到服务器.. Laravel5正在我的服务器上运行..我可以使用此方法获取数据但无法发送数据..我的代码如下:
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代码:
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);
});
服务器代码:
路线是 Route :: get('json','JsonController @ Json_login');
控制器代码
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);
}
}
但它给了我 null alert
But it giving me null alert
推荐答案
理想情况下,发送数据需要POST方法,而提取将使用GET方法。假设您想要调用名为 your_controller 的控制器并使用其 your_method 函数,让我们完成整个流程:
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 :
这适合Laravel 5
Laravel控制器:
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::post('your_url', ['uses' => 'your_controller@your_method']);
AngularJS:这是允许数据发送的代码块,它需要$ http服务。假设您的数据包含在my_data对象中。
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);
这篇关于如何将数据从角度js发送到laravel 5服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!