Despotars
Despotars

Reputation: 571

Laravel 4 - Uncaught TypeError: Cannot read property 'post' of undefined

I have this script:

<script>
    function selectModSetProd(prodId,setId,objControl){

        function ifOK(r){
            objControl.style.background="'"+r.color+"'";
        }

         function ifError(e){
            alert('An error occurred when making the request: '+e.statusText);
        }

        function petition(prodId,setId,objControl){

            var data = {
               prod: prodId,
               set: setId,
            };

            var post = $.post("sql.php", data, ifOK, 'json');
            post.error(ifError);       

        }

       var set = objControl.value;
       if(confirm("Are you sure to start "+set+"?"))

            petition(prodId,setId,objControl);          
    }
</script>

In laravel 4, but I have this error: "Uncaught TypeError: Cannot read property 'post' of undefined "

Someone could help me with this error please?

Upvotes: 0

Views: 1939

Answers (2)

Richard Torcato
Richard Torcato

Reputation: 2770

you are most likely getting the post error because your $.post is trying to go to sql.php which is not defined in your laravel routes.

update your script from sql.php to {{url().'/sql'}}

and add a sql route to laravel routes.

That's the simple solution or you can create a more restful approach to handle your ajax routes and controllers. More info on that is located at laravel.com

btw you should always wrap your jquery to make sure it loads like i posted earlier.

Upvotes: 1

Richard Torcato
Richard Torcato

Reputation: 2770

wrap your script with this

$(document).ready(function() {

});

$.post will not work until angular is loaded.

your code should read:

$(document).ready(function() {
function selectModSetProd(prodId,setId,objControl){

    function ifOK(r){
        objControl.style.background="'"+r.color+"'";
    }

     function ifError(e){
        alert('An error occurred when making the request: '+e.statusText);
    }

    function petition(prodId,setId,objControl){

        var data = {
           prod: prodId,
           set: setId,
        };

        var post = $.post("sql.php", data, ifOK, 'json');
        post.error(ifError);       

    }

   var set = objControl.value;
   if(confirm("Are you sure to start "+set+"?"))

        petition(prodId,setId,objControl);          
}
});

Upvotes: 1

Related Questions