javascript - Mturk externalQuestion 与 jquery.ajax POST : Access-Control-Allow-Origin error

标签 javascript jquery mechanicalturk

我正在使用亚马逊的 Mechanical Turk (Mturk) 并尝试提交已完成的任务,以便可以向工作人员展示下一个 HIT。

我使用 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”问题。有什么帮助吗?

最佳答案

我联系了亚马逊的乐于助人的人,他们回答说:

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>

关于javascript - Mturk externalQuestion 与 jquery.ajax POST : Access-Control-Allow-Origin error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37380921/

相关文章:

javascript - 使用ajax使用php更新mysql表

javascript - 以这种方式构建 JavaScript 的名称/术语是什么?

javascript - Kendo UI 网格和 ng 风格

javascript - 为什么 Array.indexOf() 对一个 redux Action 正确工作,但对另一个 Action 却不行? (相同的 reducer )

javascript - 使用 jQuery 从 TR 求和

validation - Mturk 外部调查代码

javascript - 在 Chrome 中,outerHeight 给出了错误的值,在 IE 和 FireFox 中则正常

javascript - 带有下拉子菜单的 Css 响应式导航

amazon-web-services - 如果位置!= 美国,则使用 Amazon MechanicalTurk?

amazon-web-services - 在 Mechanical Turk 中,如何限制每个 worker 一次 HIT