简体   繁体   English

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

[英]Grab the var from gsp to js and then controller

controller 控制者

        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 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 example 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 grabs all vars (data) just fine but the problem is that in controller the itemid seems to return null as in stacktrace. JS可以很好地捕获所有变量(数据),但是问题是在控制器中itemid似乎返回null,就像在stacktrace中一样。 I couldn't get all vars to be sent to the controller. 我无法将所有变量都发送到控制器。 I wanted to be able to find any type, portfolio, image etc, for its id to be removed from bookmark. 我希望能够找到任何类型,投资组合,图像等,以便从书签中删除其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"
    }

Testing the println but there are no messages or anything. 测试println,但没有消息或其他任何信息。 So assume that this part is not working. 因此,假设这部分不起作用。

Help appreciated! 帮助赞赏!

If I saw this correctly, you're sending the param with the name item : 如果我正确地看到了此信息,则说明您发送的参数名称为item

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

And in your controller you're looking for itemid : 在您的控制器中,您正在寻找itemid

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

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

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