简体   繁体   English

在javascript中调用控制器函数,该函数将使用ajax将条目存储在数据库中(Laravel 4)

[英]Calling a controller function in javascript that will store an entry in a database using ajax (Laravel 4)

I am trying to store an entry in a database after a click using an ajax call to a route that calls a controller function in javascript (in Laravel 4). 我试图使用ajax调用单击后在JavaScript中(在Laravel 4中)调用控制器函数的路由,将条目存储在数据库中。

I have a resource "artists", that is controlled by an "ArtistsController". 我有一个资源“艺术家”,由“ ArtistsController”控制。 The view where I am making the call is called "show.blade.php" in an "artists" directory (ie the page shows different artists: artists/1, artists/2, etc...). 我进行呼叫的视图在“艺术家”目录中称为“ show.blade.php”(即页面显示了不同的艺术家:Artists / 1,Artists / 2等)。

I also have a table called "fanartists", where I want to store this data. 我还有一个名为“ fanartists”的表格,我想在其中存储这些数据。 Basically, when a user clicks a button on a specific artist's page, I want the relationship to be stored in this table. 基本上,当用户单击特定艺术家页面上的按钮时,我希望该关系存储在此表中。

Here is the relevant code: 以下是相关代码:

show.blade.php: show.blade.php:

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId      : '*****************',
            status     : true,
            cookie     : true,
            oauth      : true
            //xfbml      : true  
    });


      $( '.opener' ).click(function() {
        FB.ui({
            method: 'feed',

            link: 'http://crowdtest.dev:8888/artists/',
            name: 'Hello',
            caption: 'Hello',
            description: 'Hello!'

            });

            request = $.ajax({
        url: "/artists/fbclick",
        type: "post",
        data: serialised data
     });

      });
};
</script>

<a class="add-list-button-no-margin opener" style="color: white; font:14px / 14px 'DINMedium','Helvetica Neue',Helvetica,Arial,sans-serif;">Play my city</a>

ArtistsController: ArtistsController:

public function fbclick($id) {

        $artist = Artist::find($id);

        $fanartist = new Fanartist;
        $fanartist->artist_id = $artist->id; //the id of the current artist page (i.e. artists/1, id=1)
        $fanartist->fan_id = Auth::user()->id;
        $fanartist->save();

    }

routes: 路线:

Route::get('/artists/fbclick', array('uses' => 'ArtistsController@fbclick'));

When I include the ajax request, the FB feed does not pop up. 当我包含ajax请求时,FB提要不会弹出。 When I remove it, it does. 当我删除它时,它会。 Also, it is not storing the data in the database like I want it to. 另外,它没有像我想要的那样将数据存储在数据库中。

Do you see anything wrong here? 您在这里看到任何问题吗? Your help is much appreciated. 非常感谢您的帮助。 Thank you. 谢谢。

I see some slight mistakes in your script. 我在您的脚本中看到一些小错误。

In your ajax request you are using post as the method, but you have defined your route as get so either change your route to post or change ajax method to get . 在您的ajax请求中,您使用post作为方法,但是您已将路线定义为get因此可以将路线更改为post或将ajax方法更改为get I will go with post route so your new route is Route::post('/artists/fbclick', array('uses' => 'ArtistsController@fbclick')'); 我将使用post路线,因此您的新路线是Route::post('/artists/fbclick', array('uses' => 'ArtistsController@fbclick')'); And ajax data field should be in json format, so now you ajax request will look some what like this 而且ajax数据字段应采用json格式,因此现在您的ajax请求将看起来像这样

$.ajax({
    url: "/artists/fbclick",
    type: "post",
    data: {field_name :'data'}
 });

Finally coming to you controller function, with silght changes 终于有了控制器功能,并进行了更改

public function fbclick() {

    // $artist = Artist::find($id);
    $id=Input::get('field_name'); //field_name is the field name from your Json data  
    $fanartist = new Fanartist;
    $fanartist->artist_id = $id; //the id of the current artist page (i.e. artists/1, id=1)
    $fanartist->fan_id = Auth::user()->id;
    $fanartist->save();

}

Everything should work now 现在一切正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM