1
Good afternoon. I have a problem with my php code developed in Laravel 4. I have a Javascript function that executes an action using $.post and if successful, calls another Javascript function, this second one using $.ajax. Both on .blade.’s pages. The problem is that when entering the second function, I suspect that the path passed in the url field is not found, but I can’t find the error. Can someone help me? Follow the codes:
javascript functions:
function form_variations_submit()
{
$.post(
'{{route('admin.ajax.save')}}',
$('#form').serialize(),
function(data) {
if(data.sucesso) {
alert("Alterações Salvas! "+data.pvid);
var pvid = data.pvid;
alert("pvid = "+pvid);
var flagOrder = envia_pvid(pvid);
alert("FlagOrder = "+flagOrder);
} else {
alert(data.erro);
}
}
);
return false;
}
function envia_pvid(id){
var pId = id;
alert("Entrou na 2 função! Envia_id "+ pId);
$.ajax({
type: 'POST',
url: '{{route('admin.ordem')}}',
data: { pId : pId
},
sucess: function(data) {
if(data.pvid){
var ret = data.pvid;
alert(ret);
return ret;
} else{
alert("ERRO!");
}
},
dataType: "json"
});
}
Route:
Route::post('p_var', array('as' => 'admin.ordem', 'uses' => 'Admin_Controller@setaOrdem'));
Function setOrder present in Controller:
public function setaOrdem()
{
$pvid = Input::get('pId');
return Response::json(array('sucesso' => true,'pvid' => $pvid));
}
The Alerts are run nomally to Flagorder Alert which returns "Undefined".
When the page is rendered what appears instead of
'{{route('admin.ajax.save')}}'
?– Guilherme Nascimento
When inspecting the element appears: http://.... /admin/ajax_save (real name of the function corresponds to admin.ajax.save)
– Joab Nunes
I will assume that the
http://....
is abbreviated. The route/admin/ajax_save
is there? How did you state it? In your question justas
foradmin.ordem
– Guilherme Nascimento
Yes '...' is the abbreviation of the url that has the name of the local server that is running the application and the folder hierarchy of the project. I declared the previous route the same way I declared the route that is not running: 'Route::post('ajax_save', array('as' => 'admin.ajax.save', 'uses' => 'Admin_controller@ajax_save'));'
– Joab Nunes
I advise you to debug in the google console Chrome.
– Ronald Araújo
This could be the problem so far as I know the folder
public
of Laravel should be used as the main path, using Virtualhost for example, if it is in a subfolder you will probably have to point it as well.– Guilherme Nascimento
then, but for the first function everything runs normally, but for the second function, located in the same folder, which executes a function in the same controller as the previous one, nothing happens. I think the error is on the route, but I may be wrong.
– Joab Nunes
It would be interesting to separate PHP from javascript. You used the Blade syntax in the middle of javascript.
– Miguel Batista