I am trying to pass objs array to a function in Laravel controller using ajax. I am not recieving any data after the post.
我正在尝试使用ajax将objs数组传递给Laravel控制器中的函数。我没有收到任何事后的数据。
<script>
var itemCount = 0;
var objs=[];
$(document).ready(function(){
var temp_objs=[];
$( "#add_button" ).click(function() {
var html = "";
var obj = {
"ROW_ID": itemCount,
"STREET_ADDRESS": $("#street_address").val(),
"CITY": $("#city").val(),
"ZIP": $("#zip").val()
}
// add object
objs.push(JSON.stringify(obj));
itemCount++;
// dynamically create rows in the table
html = "<tr id='tr" + itemCount + "'><td>" + obj['STREET_ADDRESS'] + "</td> <td>" + obj['CITY'] + " </td> <td>" + obj['ZIP'] + " </td><td><input type='button' id='" + itemCount + "' value='remove'></td> </tr>";
//add to the table
$("#multiple_table").append(html)
// The remove button click
$("#" + itemCount).click(function () {
var buttonId = $(this).attr("id");
//write the logic for removing from the array
$("#tr" + buttonId).remove();
});
});
$("#submit").click(function() {
$.ajax({
url:'/app/Http/Controllers/Search/search_address',
type: 'POST',
dataType:'json',
contentType: 'application/json',
data: objs
});
});
});
</script>
In my controller function is like this
我的控制器函数是这样的
public function search_address(){
$data = json_decode($_POST['data'], true);
print_r($data);
}
I guess that I am having a problem with the url in ajax and I am not sure how a controller's url is obtained.
我想我对ajax中的url有问题,我不确定如何获得控制器的url。
Thank you
谢谢你!
2 个解决方案
#1
0
Can you change:
你能改变:
$data = json_decode($_POST['data'], true);
to:
:
$data = json_decode(Input::get('data'));
and make sure you have: use Input;
above your class extends Controller
确保你有:使用输入;在类之上扩展控制器
See if that works.
看看这个行不行。
Edit: Also make sure your routes (in the Controllers folder) are correct.
编辑:还要确保您的路由(在controller文件夹中)是正确的。
#2
0
You should console.log()
your javascript by placing the following in you ajax post:
您应该在ajax post中放置以下内容来协调.log() javascript:
error : function(e){
console.log(e);
}
You can then see what errors you are getting in your browsers' developers tools panel.
然后可以看到浏览器的开发人员工具面板中出现了什么错误。
You should also be aware that that Laravel posts require a csrf token
unless you have explicitly turned them off, which means you will need to add this token in to your post as well. So you should end up with:
您还应该注意,Laravel的帖子需要一个csrf令牌,除非您已经显式地关闭它们,这意味着您也需要将这个令牌添加到您的帖子中。所以你应该这样结束:
$("#submit").on('click', function() {
$.ajax({
url:'/app/Http/Controllers/Search/search_address', // Is this what you meant, is this the route you set up?
type: 'POST',
data: {'data': objs, '_token' : '<?=csrf_token()?>'},
success : function(data){
// Do what you want with your data on success
},
error : function(e){
console.log(e);
}
});
});
Notice that I've embedded php
inside the javascript, which is just to illustrate the point. Ideally javascript is kept in it's own files, so you would then need to find a way to pass this token through. I personally use knockoutjs
for this type of thing (AngularJS
is also popular), but you can easily do something like:
注意,我在javascript中嵌入了php,这只是为了说明这一点。理想情况下,javascript被保存在它自己的文件中,因此您需要找到一种方法来传递这个令牌。我个人使用knockoutjs来处理这类事情(AngularJS也很受欢迎),但是你可以轻松地完成以下事情:
<input type="hidden" id="_token" value="{{ csrf_token() }}" />
in your HTML, then pull this value from inside your ajax request:
在HTML中,然后从ajax请求中提取这个值:
data: {'data': objs, '_token' : $('#_token').val()}
EDIT
编辑
I've just noticed your url, it looks like you are trying to access the controller directly. You need to set up a route in your routes.php
file, such as:
我刚注意到你的url,看起来你想直接访问控制器。你需要在你的路线上建立一条路线。php文件,例如:
Route::post('/searchAddress', 'YourController@search_address');
Then use:
然后使用:
url: /searchAddress
in your ajax request.
在您的ajax请求。
#1
0
Can you change:
你能改变:
$data = json_decode($_POST['data'], true);
to:
:
$data = json_decode(Input::get('data'));
and make sure you have: use Input;
above your class extends Controller
确保你有:使用输入;在类之上扩展控制器
See if that works.
看看这个行不行。
Edit: Also make sure your routes (in the Controllers folder) are correct.
编辑:还要确保您的路由(在controller文件夹中)是正确的。
#2
0
You should console.log()
your javascript by placing the following in you ajax post:
您应该在ajax post中放置以下内容来协调.log() javascript:
error : function(e){
console.log(e);
}
You can then see what errors you are getting in your browsers' developers tools panel.
然后可以看到浏览器的开发人员工具面板中出现了什么错误。
You should also be aware that that Laravel posts require a csrf token
unless you have explicitly turned them off, which means you will need to add this token in to your post as well. So you should end up with:
您还应该注意,Laravel的帖子需要一个csrf令牌,除非您已经显式地关闭它们,这意味着您也需要将这个令牌添加到您的帖子中。所以你应该这样结束:
$("#submit").on('click', function() {
$.ajax({
url:'/app/Http/Controllers/Search/search_address', // Is this what you meant, is this the route you set up?
type: 'POST',
data: {'data': objs, '_token' : '<?=csrf_token()?>'},
success : function(data){
// Do what you want with your data on success
},
error : function(e){
console.log(e);
}
});
});
Notice that I've embedded php
inside the javascript, which is just to illustrate the point. Ideally javascript is kept in it's own files, so you would then need to find a way to pass this token through. I personally use knockoutjs
for this type of thing (AngularJS
is also popular), but you can easily do something like:
注意,我在javascript中嵌入了php,这只是为了说明这一点。理想情况下,javascript被保存在它自己的文件中,因此您需要找到一种方法来传递这个令牌。我个人使用knockoutjs来处理这类事情(AngularJS也很受欢迎),但是你可以轻松地完成以下事情:
<input type="hidden" id="_token" value="{{ csrf_token() }}" />
in your HTML, then pull this value from inside your ajax request:
在HTML中,然后从ajax请求中提取这个值:
data: {'data': objs, '_token' : $('#_token').val()}
EDIT
编辑
I've just noticed your url, it looks like you are trying to access the controller directly. You need to set up a route in your routes.php
file, such as:
我刚注意到你的url,看起来你想直接访问控制器。你需要在你的路线上建立一条路线。php文件,例如:
Route::post('/searchAddress', 'YourController@search_address');
Then use:
然后使用:
url: /searchAddress
in your ajax request.
在您的ajax请求。