呼叫laravel CONTROLER从用GET / PUT一个Ajax请求/删除方法方法、CONTROLER、laravel、GET

2023-09-10 14:55:27 作者:゛×尐诗妹罒

我有这样的一个JavaScript,将调用两个差异情况下两种不同的控制器.... 现在的问题是,它是GET方法,一个是破坏的方法......生成呼叫时,控制器的功能不被触发。不知道wheather的URL方法正确与否。这是给500错误。

我也不能确定响应从控制器背面。但在此之前,我需要执行的控制器......这是不会执行数据库操作。提前感谢您的帮助:)

JS code以下: - >

 函数follow_or_unfollow(ID,动作)
{
    // VAR dataString =ID =+ ID;
    如果(行动==跟随){
        myUrl ={{行动(FollowsController @秀','身份证')}};
        在myMethod =GET;
    }
    其他
    {
        myUrl ={{行动(FollowsController @灭','身份证')}};
        在myMethod =删除;

    }
    VAR dataString =ID =+ ID;
    $阿贾克斯({
        类型:myMethod的,
        网址:myUrl,
        //数据:dataString,
        beforeSend:函数()
        {
            如果(行动==下面)
            {
                $(#下面的+ id)的.hide();
                $(#加载+ ID)的.html('< IMG SRC =loading.gifALIGN =absmiddleALT =载入中...>');
            }
            否则,如果(行动==跟随)
            {
                $(#后续+ id)的.hide();
                $(#加载+ ID)的.html('< IMG SRC =loading.gifALIGN =absmiddleALT =载入中...>');
            }
            其他 { }
        },
        成功:函数(响应)
        {
            如果(行动==下面){
                $(#加载+ ID)的.html('');
                $(#后续+ id)的.show();

            }
            否则,如果(行动==跟随){
                $(#加载+ ID)的.html('');
                $(#下面的+ id)的.show();
            }
            其他 { }
        },
        错误:函数(XHR,textStatus,errorThrown){

                    如果(xhr.status == 500){
                        警报(错误500:+ xhr.status +:+ xhr.statusText);

                    } 其他 {
                        警报(错误:+ xhr.status +:+ xhr.statusText);

                    }
                },
                完成:功能(XHR,textStatus){

                      阿勒特('OK');

                }

    });
}
< / SCRIPT>
 

第一次修改

拿到一分后,Firefox的工具清理(所建议的Chinnu)

据显示

 `[18:38:47.658] GET的http://本地主机/ ffdd /公/如下/ ID [HTTP / 1.1 500内部服务器错误230ms]`
 

这意味着什么!该ID生成的数字或id变量,它只是要为ID字符串。现在需要一个具体的解决方案,如何发送的id的值有....在控制器througn的JS。

第二次修改

laravel教程 第一章安装laravel

如果我把通过'身份证',这意味着,如果id的值,可以直接打印有它的工作提它的数字或ID的值来代替。会有什么简单的方法来做到这一点?

  myUrl ={{行动(FollowsController @灭',< ID&GT的价值;)}};
 

解决方案

您试图访问一个PHP辅助功能将无法正常工作里面的一个JavaScript变量。我不知道什么是合格ID到该功能,但如果它被传递到通过PHP的观点,那么你应该使用 {{行动(FollowsController @秀,数组($ ID))} }

如果它没有被传递到视图,那么你将不得不使用 myUrl ='/跟随/+ ID ,并确保您有两条路线 GET 删除

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>) }}" ;

解决方案

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.