2017-09-13 72 views
0

单击元素时,我有一个ajax调用触发器。阿贾克斯去控制器。更改操作名称时请求的资源不可用

function(){ 

    $.ajax({ 
        url: "/GAIA/RemoveCE/removeFlow", 
        type: "get", 
        data: {id:id}, 
        cache: false, 
        async: true, 
        success: function() { 
         window.location.reload() 
        }, 
        error: function(xhr) { 
         console.log("ERROR"); 
         console.log(xhr.readyState); 
         console.log(xhr.status); 
        } 

       }); 
       event.preventDefault(); 
      }); 

我在这个控制器上有一个名为“removeFlow”的动作和一个名为“removeMachine”的动作。

def removeFlow(Long id){ 
    println id 
} 

def removeMachine(Long id){ 
    println id 
} 

当调用ajax来触发“removeFlow”时,不会打印任何内容。

当调用ajax来触发“removeMachine”时,打印正确。

两次调用收到的错误消息是404资源不可用,我明白,因为我没有实现一个视图,我不重定向。我不明白为什么没有打印。

这是Grails的2.5.6运行

编辑:

这是我在开发者控制台上获得(被点击时元和Ajax被触发)错误:

jQuery的1.11 ?.1.js编译=假&编码= UTF-8:9631 GET http://localhost:8080/GAIA/removeCE/removeFlow?id=41&_=1505308707652 404(未找到)

如果我手动输入URL(/ GAIA/removeCE/removeFlow ID = 41?)1得到这样的:

:8080/GAIA/removeCE/removeFlow ID = 41:1 GET http://localhost:8080/GAIA/removeCE/removeFlow?id=41 404(未找到)

+0

这是最有可能的资源是不可用的服务器上获取地址在阿贾克斯,或在不同的URL上,首先使用浏览器确认URL路径。 –

回答

0

在您需要渲染的东西作为对AJAX调用的响应Groovy的动作,否则你的电话转到成功,但它会返回404错误。 更改您的代码,如下所示。

def removeFlow(Long id){ 
    println id 
    // render anything string or (List/Map as Json) 
    render "Deleted Id:"+id+" successfully" 
} 


def removeMachine(Long id){ 
    println id 
// render anything string or (List/Map as Json) 
    render "Deleted Id:"+id+" successfully" 
} 

//接收在阿贾克斯成功响应,

success: function(response) { 
     console.log(response) 
    //OR 
    alert(response); 
} 

//使用链接的taglib

url: '<g:createLink controller="removeCE" action="removeFlow"/>' 
+0

我明白你的意思,当我输入你的代码并改变ajax调用来调用“removeMachine”时,它呈现正常。 但是,如果我打电话“removeFlow”它仍然给我404资源不可用错误。 这似乎很奇怪,我可能忽略了一些简单的事情 – jacob88

+0

@ jacob88不错..! –

+0

因此,不幸的是问题仍然存在:-) – jacob88