Lluís Puig Ferrer Lluís Puig Ferrer - 1 year ago 44
Javascript Question

Ajax isn't working

i'm trying to do a request with ajax and isn't working. The controllers have the correct code, cause if i do it with ajax they are working good.

The views is this:




$(document).ready(function(){
listProject();
$("#buttoncreate").click(function(e)){
e.listUploadProject();
});
});

var listProject = function()
{
$.ajax({
type:'get',
url:'{{ url('admin/project/listall') }}',
success: function(data){
$('#ajaxwindow').empty().html(data);
}
});
}
var listUploadProject = function()
{
$.ajax({
type:'get',
url:'{{ url('admin/project/create') }}',
success: function(data){
$('#ajaxwindow').empty().html(data);
}
});
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="col-md-10">
<h3 style="letter-spacing:40px;text-align:center;color:f15d5e;">PROYECTOS</h3>
</div>
<div class="col-md-2" style="padding:20px;">
<button type="button" id="buttoncreate" class="btn btn-danger">Crear Proyecto</button>

</div>
<div class="col-md-12" id="ajaxwindow">

</div>





If i only have:

$(document).ready(function(){
listProject();
});


Is working good.

Where's the problem? thanks!.

EDIT:

$(document).ready(function(){
listProject();
$("#buttoncreate").click(function(e){
e.preventDefault();
e.listUploadProject();
});
});

var listProject = function()
{
$.ajax({
type:'get',
url:"{{ url('admin/project/listall') }}",
success: function(data){
$('#ajaxwindow').empty().html(data);
}
});
}
var listUploadProject = function()
{
$.ajax({
type:'get',
url:"{{ url('admin/project/create') }}",
success: function(data){
$('#ajaxwindow').empty().html(data);
}
});
}


When i click to the button and he try to do the function give me this error:

Uncaught TypeError: e.listUploadProject is not a function
at HTMLButtonElement.<anonymous> (projects:217)
at HTMLButtonElement.dispatch (jquery.js:4737)
at HTMLButtonElement.elemData.handle (jquery.js:4549)


Maybe something wrong with the call to listUploadProject? thanks!

Answer Source

It's because you prematurely close your quotes. Escape them using backslash (\)

like this:

url: '{{url(\'admin/project/create\')}}'

or you can mix up the quotes:

url: "{{url('admin/project/create')}}"

update 1

To answer that last error:

change function to

$("#buttoncreate").click(function()
{
    listUploadProject();
});

this will run the function on click.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download