2016-05-23 65 views
1

我使用Amazons Mechanical Turk(Mturk)並嘗試提交一個完整的任務,以便工作人員可以顯示下一個HIT。Mturk externalQuestion與jquery.ajax POST:訪問控制 - 允許來源錯誤

我使用externalQuestion和我的服務器存儲工作人員輸入的所有數據。後一個工人完成,他們點擊提交按鈕,發送下面的POST:

$.ajax({ 
    url: self.props.userData.turkSubmitTo + '/mturk/externalSubmit', 
    data: {assignmentId:self.props.userData.assignmentId}, 
    type: 'POST', 
    success: function(resp) { console.log('good');}, 
    error: function(resp, err) { console.log('fail'); console.log(resp); console.log(err);} 
}); 

不幸的是,我得到以下錯誤:

XMLHttpRequest cannot load https://workersandbox.mturk.com/mturk/externalSubmit?assignmentId=3AMYWKA6YBMSYVY7OGYPJIPCGPK6OK. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://shrouded-plains-8041.herokuapp.com' is therefore not allowed access. 

需要注意的是,如果我發佈與數據類型「JSONP」嘗試解決這個明顯的跨域問題,然後提交工作正常,但externalQuestion iframe不會刷新到下一個HIT。

我不知道爲什麼我得到這個跨域'Access-Control-Allow-Origin'問題。請幫忙嗎?

+0

請不要爲錯誤在網上搜索。它每天在這裏出現很多次。至於使用jsonp,你不能使用'POST'和jsonp,因爲它是一個腳本請求而不是ajax。簡短的回答是你需要在API上啓用CORS,如果可能或使用代理 – charlietfl

+0

好的謝謝!是的,我確實做了一些Google搜索。這對jsonp是有意義的,爲什麼它不能按預期工作。我無法訪問api來啓用COR。我想它必須是一個代理。 看起來很奇怪,亞馬遜的Mturk需要一個代理來完成這個相當標準的操作(而這種陌生性就是爲什麼我會問這個問題)。任何有Mturk的人都可以評論嗎? – Yetti

回答

1

我與亞馬遜樂於助人的民俗取得了聯繫,他們的回答是:

Hi Ben -

I believe the problem is that your code is attempting to get the user's browser to submit an AJAX request to Amazon. Since the page with this code is being generated by your own app on Heroku, the browser does not allow this by default (making AJAX calls from one domain to another).

The solution is to have the code do a form submit, not an AJAX submit. See documentation for JQuery's form submit here: https://api.jquery.com/submit/ .

Let me know if that works for you.

Thanks, Taneem

這裏現在是提交代碼:

<rb.Modal.Footer> 
    <form name="mturk_form" method="post" id="mturk_form" action={self.props.userData.submitTo + "/mturk/externalSubmit"}> 
     <input type="hidden" value='' name="assignmentId" id={self.props.userData.assignmentId}/> 
     <rb.Input type="submit" style={{width:'70%', float: "left"}}/> 
     <rb.Button onClick={self.props.closeSubmit}>Cancel</rb.Button> 
    </form> 
相關問題