[英]JQuery Ajax returning success with nonexistent url parameter
So I have this code in one of my Laravel's views: (there is no such url as "nonexistenturl", I just made it up) 因此,在Laravel的一种观点中,我有这段代码:(没有“ unexistenturl”这样的网址,我只是整理了一下)
<script type="text/javascript">
$(document).ready(function(){
$('#btnAdd').click(function(){
addToCart();
});
function addToCart(){
$.ajax({
method: "GET",
url: "nonexistenturl",
data: { msg: 'hi' },
success: function(data){
alert('success');
}
})
}
});
</script>
And the alert message is showing, I don't understand why. 而且显示警告消息,我不明白为什么。 Thanks for the help! 谢谢您的帮助!
Edit 0: Now I tried the same code in another View and I get an error as it must be, but I don't know why it works in the other view yet. 编辑0:现在,我在另一个视图中尝试了相同的代码,但出现了必须的错误,但是我不知道为什么它在另一个视图中仍然有效。
Edit 1: 编辑1:
For some reason if I run the javascript code I posted from one specific view, the following code is being executed: 由于某种原因,如果我运行从一个特定视图发布的javascript代码,则会执行以下代码:
public function show($id)
{
$recipe = \App\Recipe::find($id);
return view('recipes.show')->with('recipe', $recipe);
}
The Laravel's route for this is: Laravel的路线是:
Route::get('recipe/{id}', 'RecipesController@show');
So it's as if the ajax request was using another url, the one from the current view... 因此,就好像ajax请求正在使用另一个URL,即当前视图中的那个一样。
Edit 2 编辑2
I feel so stupid now. 我现在真傻。 As charlietfl suggested it was using the url "recipe/nonexistent". 正如charlietfl建议的那样,它使用的是“食谱/不存在”网址。 So all I have to do is use: url: "{{url('route')}}" 所以我所要做的就是使用:url:“ {{url('route')}}”
Thanks for your help everyone :) 谢谢大家的帮助 :)
This probably mean that Laravel is returning some type of web page, even if there is no data on it, it returning something would be considered a success. 这可能意味着Laravel正在返回某种类型的网页,即使上面没有数据,它返回的内容也被认为是成功的。 A good way to debug this would be to alert the data, and it if returns an object then alert data.responseText. 调试此错误的一个好方法是警告数据,如果返回一个对象,则警告data.responseText。
alert(data.responseText);
It would also help to navigate the any fake urls you are making to see what Laravel returns on them. 这也将有助于导航您正在制作的任何伪造的URL,以查看Laravel返回的内容。 EDIT 编辑
Try specifying json as the return type, I believe it defaults to text. 尝试将json指定为返回类型,我相信它默认为text。
$.ajax({
method: "GET",
url: "nonexistenturl",
dataType: 'json',
data: { msg: 'hi' },
success: function(data){
alert('success');
}
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.