Call laravel controler from an ajax request with get / put / delete method

六眼飞鱼酱① 提交于 2019-12-20 06:14:24

问题


I am having a javascript like this which will call two different controller in two diff situation .... Now the problem is it is one get method and one is destroy method ... when the call is generated, the controller functions never triggered. No idea wheather the url method is right or not. It is giving the 500 error.

I am also not sure about the response back from the controller. but before that I need to execute the database operation in the controller ... which is not going to execute. advance thanks for the help :)

JS code below :-->

function follow_or_unfollow(id,action)
{
    //var dataString = "id=" + id;
    if( action == "follow" ) {
        myUrl = "{{ action('FollowsController@show', 'id') }}" ;
        myMethod = "GET";
    }
    else
    { 
        myUrl = "{{ action('FollowsController@destroy', 'id') }}" ;
        myMethod = "DELETE";

    } 
    var dataString = "id=" + id;
    $.ajax({  
        type: myMethod ,  
        url: myUrl,  
        //data: dataString,
        beforeSend: function() 
        {
            if ( action == "following" )
            {
                $("#following"+id).hide();
                $("#loading"+id).html('<img src="loading.gif" align="absmiddle" alt="Loading...">');
            }
            else if ( action == "follow" )
            {
                $("#follow"+id).hide();
                $("#loading"+id).html('<img src="loading.gif" align="absmiddle" alt="Loading...">');
            }
            else { }
        },  
        success: function(response)
        {
            if ( action == "following" ){
                $("#loading"+id).html('');
                $("#follow"+id).show();

            }
            else if ( action == "follow" ){
                $("#loading"+id).html('');
                $("#following"+id).show();
            }
            else { }
        },
        error : function(xhr, textStatus, errorThrown ) {

                    if (xhr.status == 500) {
                        alert("Error 500: "+xhr.status+": "+xhr.statusText);

                    } else {
                        alert("Error: "+xhr.status+": "+xhr.statusText);

                    }
                },
                complete : function(xhr, textStatus) {

                      allert('ok');

                }

    }); 
}
</script>

1st edit

get a point after the firefox tool check up (as suggested by Chinnu)

It is showing

`[18:38:47.658] GET http://localhost/ffdd/public/follows/id [HTTP/1.1 500 Internal Server Error 230ms]`

that means ! the id is not been generated as digit or id variable, it is just going as "id" string. Now need a specific solution , how to send the value of id there .... at the controller througn the JS.

2nd edit

If I put the digit or value of id instead of mentioning it by 'id' that means if the value of id can be print directly there it will work . What will be the easier way to do that ?

myUrl = "{{ action('FollowsController@destroy', <the value of id>) }}" ;

回答1:


The type of request to make ("POST" or "GET"), default is "GET". Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

Ref: http://api.jquery.com/jQuery.ajax/

NOTE: You can able to find this errors using in Firefox Tools->Web Developer ->Error Console or CTRL+SHIFT+J




回答2:


You are trying to access a javascript variable inside a php helper function will not work. I'm not sure what's passing id into that function, but if it's being passed into the view via PHP, then you should use {{ action('FollowsController@show', array($id)) }}

If it's not being passed into the view, then you would have to use myUrl = '/follow/'+id and make sure you have routes for both GET and DELETE.



来源:https://stackoverflow.com/questions/20174667/call-laravel-controler-from-an-ajax-request-with-get-put-delete-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!