我看了很多帖子,似乎没有一个可以解决这个问题。
我正在尝试通过 ajax 调用从 javascript 发送数据,然后将该数据放入服务器级别的 grails 控制器/操作中以进行处理,然后显示该网页。数据传输正常,但目标网页无法替换当前网页。
这是成功将数据发送到操作的 ajax 调用:
$.ajax({
type: "POST",
url: "createVideopNm",
dataType: "json",
contentType: "application/json",
asynch: false,
processData: false,
data: vidData
//data: vidAudioId,
// data: { name: "Mike", location: "UK" },
})
.done(function(msg, status, error) {
alert( "getJsVCoords Done: data SAVED - Sys status: " + status
+ "\n" +
" Error: " + error
+ "\n" +
" responseText: " + msg.responseText
)
})
.error(function(msg, status, error) {
alert( "getJsVCoords Error: Data NOT Saved -1- Sys status: " + status
+ "\n" +
" Error: " + error
+ "\n" +
" responseText: " + msg.responseText
)
})
这是肯定从 ajax 调用运行的控制器操作:
def createVideopNm(Integer max) {
if(request.method == 'GET')
{
log.debug(" createVideopNm - GET ")
}
else if(request.method == 'POST')
{
// POST called from javascript functrionsendVidData in pNm
log.debug(" createVideopNm - POST -1-")
def jsonObject = request.JSON
log.debug(" createVideopNm - POST -2-" + jsonObject.xCoord)
render ( ['X': jsonObject.xCoord , 'Y': jsonObject.yCoord , 'P' : jsonObject.picId, 'A' : jsonObject.audioId] as JSON)
log.debug(" createVideopNm - POST -3-")
}
def workVideo= prepVideopNm()
flash.message = "createVideopNm workVideo nm: ${workVideo}"
log.debug(" createVideopNm - -4-")
params.max = Math.min(max ?: 10, 100)
log.debug(" createVideopNm - -5-")
[workVideo: workVideo]
log.debug(" createVideopNm - -6-")
return true
}
正如您所看到的,我有一个通过操作的 GET 和 POST 路径 - 如果我只是通过在浏览器地址字段中输入 URL 来调出页面,它就可以正常工作。但是,如果我通过 ajax 调用调用该操作,它不会出现 - 所有日志记录都表明一切都很好。例如,Chrome 网络诊断工具 Console 有一条消息——“XHR 完成加载:' url name '
比较 GET 和 POST/ajax 操作的 grails 日志文件,GET 有一些额外的 grail 日志消息,这反映了它确实正确显示网页的事实:
2013-05-16 20:05:42,104 [http-bio-9000-exec-8] DEBUG homevu1.VideosController - createVideopNm - -4-
2013-05-16 20:05:42,104 [http-bio-9000-exec-8] DEBUG homevu1.VideosController - createVideopNm - -5-
2013-05-16 20:05:42,104 [http-bio-9000-exec-8] DEBUG homevu1.VideosController - createVideopNm - -6-
2013-05-16 20:05:42,215 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - resolveResourceAndURI: [dir:images, file:favicon.ico]
2013-05-16 20:05:42,216 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - resolveResourceAndURI: [dir:images, file:apple-touch-icon.png]
2013-05-16 20:05:42,217 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - resolveResourceAndURI: [dir:images, file:apple-touch-icon-retina.png]
2013-05-16 20:05:42,218 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - resolveResourceAndURI: [dir:css, file:main.css]
2013-05-16 20:05:42,219 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - resolveResourceAndURI: [dir:css, file:mobile.css]
2013-05-16 20:05:42,219 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - laying out resources for request org.apache.catalina.connector.RequestFacade@275a7e94: [:]
2013-05-16 20:05:42,220 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - Rendering resources for disposition [head]
2013-05-16 20:05:42,220 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - Rendering resources, modules in tracker: null
2013-05-16 20:05:42,220 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - Rendering resources, modules needed: []
2013-05-16 20:05:42,220 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - Rendering non-deferred resources, modules: []...
2013-05-16 20:05:42,220 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - Rendering page fragments for disposition: head
2013-05-16 20:05:42,220 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - Removing outstanding request disposition: head
2013-05-16 20:05:42,221 [http-bio-9000-exec-8] DEBUG resource.ResourceTagLib - resolveResourceAndURI: [dir:HomeVu1/skin, file:HomeVu.jpg, absolute:false]
而通过 ajax 操作的 POST 记录只是在“createVideopNm - -6-”消息之后停止。
通过 ajax 调用而不是直接通过在浏览器中输入 url 地址来调用 grails 操作时的行为有什么不同?