Laravel 4 jquery Ajax 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17577555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:07:11  来源:igfitidea点击:

Laravel 4 jquery Ajax Request

ajaxlaravel

提问by Ranjan

I am new to Laravel 4. I have a page within which I have 3 tabs(basically big clickable divs) to load the different user information. What I would like to do is that I would like to be able to click on those tabs,make jquery ajax request,bring back a partial view and update a part of page with that partial view. I would like to able to do that for every tab.

我是 Laravel 4 的新手。我有一个页面,其中有 3 个选项卡(基本上是可点击的大 div)来加载不同的用户信息。我想要做的是我希望能够点击这些选项卡,发出 jquery ajax 请求,带回部分视图并使用该部分视图更新页面的一部分。我希望能够为每个标签做到这一点。

I got the click event firing.I also tried different code to accomplish ajax request. For some reason, nothing happens. I do not know if I am missing something in route.php. Could anyone please give me some code on how to do that? or may be some other idea?

我得到了点击事件的触发。我还尝试了不同的代码来完成 ajax 请求。出于某种原因,什么也没有发生。我不知道我是否在 route.php 中遗漏了什么。谁能给我一些关于如何做到这一点的代码?或者可能是其他想法?

I did this::

我这样做了::

Route.php

路由.php

Route::post('userInfo','UserController@showInfo');

jqueryAjax:

jqueryAjax:

$("divtoUpdatePartialView").click(function(){

$.ajax({
  type: "POST",
  url:Not Sure,
  data: { id: userId }
}).done(function( msg ) {
  alert( msg );
});

}

I tried using Base Url too, but nothing happens. I have an ajax library declared on my master page. Could you please help me or give me some other ideas?

我也尝试使用 Base Url,但没有任何反应。我在我的母版页上声明了一个 ajax 库。你能帮我或给我一些其他的想法吗?

Thank you very much for your answer. I tried that way, this is what I have...

非常感谢您的回答。我试过了,这就是我所拥有的......

Route.php:

Route::get('user_Profile/userInfo', 'UserController@getshow');

The ajax:

阿贾克斯:

var  userId='1';

$.ajax({
    type: "POST",
    url: 'user_Profile/userInfo',
    data: { id: userId }
}).done(function( msg ) {
    alert( "the messageis "+msg );
});


My userController::

public function getshow()
    {
           return "No ajax";
    if (Request::ajax()) {

        return "yeahhhh";
    $html=View::make('user.userProfile')->nest('partial', 'user.userInfoPartial')-        >with('title',$title)->render();


}
}

When I directly access the page I receive "No ajax", but when I try the ajax-way,nothing happens.Could you see what I am missing??

当我直接访问页面时,我收到“No ajax”,但是当我尝试使用 ajax 方式时,什么也没有发生。你能看到我遗漏了什么吗?

Thank you again for your help..

再次感谢你的帮助..

采纳答案by The Alpha

The urlshould be the path/pattern you have used in your routeand in this case it's

url应该是你已经在你使用的路径/模式route,在这种情况下,它

userInfo

The ajax

ajax

$.ajax({
    type: "POST",
    url: 'userInfo',
    data: { id: userId }
}).done(function( msg ) {
    alert( msg );
});

Also, make sure that, you can directly access the page from your browser's address bar using

此外,请确保您可以使用浏览器的地址栏直接访问该页面

http://yourdomain.com/userInfo

回答by Pawel

return sentence should go AFTER the if sentence;

return 语句应该在 if 语句之后;

   public function getshow(){

    if (Request::ajax()) {
        $html=View::make('user.userProfile')->nest('partial', 'user.userInfoPartial')-        >with('title',$title)->render();
        return "yeahhhh";
    }
           return "No ajax";
}

Return sentence exits the function, that means that any further code is not going to be executed :)

返回语句退出函数,这意味着不会执行任何进一步的代码:)