将var从gsp抓取到js然后控制器

时间:2014-04-03 17:06:08

标签: javascript jquery grails

控制器

        class BookmarkItemApiController {

        static responseFormats = ['json', 'xml']

        static allowedMethods = [delete:['POST', 'DELETE'], update:['POST', 'PUT']]

        def index() {
            render (text:"test")
        }

        @Transactional
        def deleteItem(Bookmark bookmark) {

            if (bookmark == null) {
                render status: NOT_FOUND
                return
            }

    ///not working here ////
            def itemid = params.itemid
            println " --> itemid -- ${itemid}"

            if (params.portfolio) {
                itemid.delete()
                println ">>> delete portfolio id"
            } else if (params.catalogue) {
                itemid.delete()
                println ">>> delete  id"
            } else if (params.image) {
                itemid.delete()
                println ">>> delete image id"
            }
    //////////////////////////
           def item = [:]
            item.name = bookmark.name
            item.id = bookmark.id
            item.delete = true

            item.items = []
            item.items += bookmark.portfolios?:[]
            item.items += bookmark.images?:[]
            item.items += bookmark.catalogues?:[]


            respond item

        }

JS

$('.ac-hns').on('click', '.icn-close-white', function(e) {

        e.preventDefault();
        deleteBookmarkItem($(this), $(this).parent().data('id'), $(this).parent().data('type'), $(this).parent().attr('data-bookmark-id'));
    });
 function deleteBookmarkItem(btn, itemID, category, bookmarkID) {
        var sendData = {
            item: itemID,
            // ownerId: userSessionId
        };
        sendData[category] = itemID;
        console.log(sendData)

        console.log('?: ' + $.param(sendData));

        $.ajax({
            url: '/bookmarkItemApi/deleteItem/' + bookmarkID + '?' + $.param(sendData),
            type: 'GET',
            contentType: 'application/json',
            dataType: 'json',
            success: function(response) {
                //unlink or remove portfolio from bookmark
                $('[data-id="' + itemID + '"]').remove();
                console.log(response);
            },
            error: function(error) {
                console.log(error);
                console.log('fails');
            }
        });
    }

GSP示例

     <g:each in="${bookmark.loadCatalogues()}" var="catalogue">
  <div class="mb ac-hns" data-id="${catalogue.id}" data-bookmark-id="${bookmark.id}" data-type="catalogue">
  <a href="/catalogue/more/${catalogue.id}">
    <span>
      <span>${catalogue.title}</span>
      <span>By ${catalogue.owner?.fullname}</span>
    </span>
    <span>
      <span>love
    </span>
  </a>
  <i class="icn-close-white"></i>
</div>
</g:each>

JS抓住所有变量(数据)就好了,但问题是在控制器中,itemid似乎在stacktrace中返回null。我无法将所有变量发送到控制器。我希望能够找到任何类型,投资组合,图像等,以便从书签中删除其ID。

        def itemid = params.itemid
    println " --> itemid -- ${itemid}"

    if (params.portfolio) {
        itemid.delete()
        println ">>> delete portfolio id"
    } else if (params.catalogue) {
        itemid.delete()
        println ">>> delete  id"
    } else if (params.image) {
        itemid.delete()
        println ">>> delete image id"
    }

测试println但没有消息或任何东西。所以假设这部分不起作用。

帮助表示赞赏!

1 个答案:

答案 0 :(得分:0)

如果我看到了这一点,您就会发送名为item的参数:

var sendData = {
  item: itemID,
  // ownerId: userSessionId
};

在您的控制器中,您正在寻找itemid

///not working here ////
def itemid = params.itemid
println " --> itemid -- ${itemid}"