我有这样一个JavaScript,这将调用两个不同的控制器在两个不同的情况.... 现在的问题是它是一个获取方法和一个是销毁方法...当生成调用时,控制器功能从未触发。不知道URL方法是否正确。这是给500错误。从ajax请求调用laravel控制器与get/put/delete方法
我也不确定控制器的回应。但在此之前,我需要在控制器中执行数据库操作......这不会执行。提前感谢您的帮助:)
下面JS代码: - >
function follow_or_unfollow(id,action)
{
//var dataString = "id=" + id;
if(action == "follow") {
myUrl = "{{ action('[email protected]', 'id') }}" ;
myMethod = "GET";
}
else
{
myUrl = "{{ action('[email protected]', '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>
1日编辑
拿到一分Firefox的工具清理后(由Chinnu的建议)
它表示
`[18:38:47.658] GET http://localhost/ffdd/public/follows/id [HTTP/1.1 500 Internal Server Error 230ms]`
那手段!该id没有被生成为数字或id变量,它只是作为“id”字符串。现在需要一个特定的解决方案,如何发送id的值....在控制器通过JS。
第二编辑
如果我把数字或ID的价值,而不是通过“ID”,这意味着如果id的值可以直接打印有它的工作提到这一点的。更简单的方法是什么?
myUrl = "{{ action('[email protected]', <the value of id>) }}" ;
感谢您的回复兄弟! 这很好。但是如果我们在这里考虑get或post方法,那也没有被触发到控制器!你能在这个JS代码中找到任何错误吗? –
Firefox Tools-> Web Developer - >错误控制台或CTRL + SHIFT + J –
您的代码显示 - 错误:405:方法不允许 –