2010-07-21 41 views
1

我在使用Firefox時向服務器發送發佈數據時遇到問題。 服務器正在Google App Engine上運行。JavaScript選項使用Firefox發送跨站點數據

這是我在我的JavaScript中。

$.ajax({
url: 'http://someurl/example/myform.json',
type: 'post',
dataType: 'json',
data: {
'value.title': title,
'value.info.first': first,
'value.info.second': value
}, success: function(data) {
alert("success");
},
error: function(object, status, error) {
alert("error");
}
});

並在服務器上我有。

@RequestMapping(value = "/myform.json", method = RequestMethod.POST)
public ResponseEntity<String> create(@ModelAttribute("data") @Valid final Data data,
final BindingResult result, final HttpServletResponse resp,) {
//process Data }

到目前爲止好,這對IE和Chrome的工作沒有問題。 但後來我發現它不適用於Firefox,這是因爲瀏覽器在發佈任何內容之前首先發送OPTIONS方法,所以我繼續向服務器發送以下內容。

@RequestMapping(value = "/form.json", method = RequestMethod.OPTIONS)
public ResponseEntity<String> options( final HttpServletResponse resp) {
final HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Access-Control-Allow-Origin", "*");
responseHeaders.set("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
responseHeaders.set("Access-Control-Allow-Headers", "Content-Type");
responseHeaders.set("Access-Control-Max-Age", "86400");
return new ResponseEntity<String>("",responseHeaders,HttpStatus.OK);
}

這裏的問題是,這返回500並且日誌顯示警告。

`java.security.AccessControlException: access denied (java.lang.RuntimePermission accessDeclaredMembers)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:355)
at java.security.AccessController.checkPermission(AccessController.java:567)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at com.google.apphosting.runtime.security.CustomSecurityManager.checkPermission(CustomSecurityManager.java:45)
at java.lang.SecurityManager.checkMemberAccess(Unknown Source)
at java.lang.Class.checkMemberAccess(Unknown Source)
at java.lang.Class.getDeclaredMethods(Unknown Source)

`

有什麼建議?

回答

0

在$阿賈克斯電話,你可以嘗試明確設置的contentType,即:

$.ajax({ 
    url: 'http://someurl/example/myform.json', 
    type: 'post', 
    dataType: 'json', 
    contentType: 'application/json', 
    data: { 
     'value.title': title, 
     'value.info.first': first, 
     'value.info.second': value 
    }, complete: function() { 
     alert("done"); 
    } 
}); 

我知道,我總是做這與X-站點的呼叫,也做了相當長的一段時間,由於您描述的問題類型(在呼叫在服務器上彙編之前需要首先整理OPTIONS)。

我敢肯定,這將讓你的舞臺。此外,如果沒有成功..

吉姆

+0

感謝您的快速答覆。不幸的是,我無法讓這個工作,它實際上使Chrome開始導致500條消息。令人費解的說至少... – user397147 2010-07-21 09:28:41

+0

嗯 - 不知道那可能是什麼。可能是一個想法,包括$ .ajax錯誤:函數(即錯誤:函數(請求,狀態,錯誤){...}) – 2010-07-21 10:59:01

+0

經過一些測試後,似乎即使服務器日誌和篡改數據顯示響應返回作爲500它進入成功:每次函數和返回的數據爲空。 – user397147 2010-07-21 11:45:13