Javascript 将ajax帖子发送到FormSpree.io

Javascript 将ajax帖子发送到FormSpree.io,javascript,jquery,ajax,angularjs,Javascript,Jquery,Ajax,Angularjs,我目前有一个jQuery脚本,它将ajax发送到(静态表单提交)。他们的医生说要做到这一点: $.ajax({ url: "//formspree.io/you@email.com", method: "POST", data: {message: "hello!"}, dataType: "json" }); data: 'first=hgf&last=ghfgh', headers: {'Content-Type': 'application/x-www-for

我目前有一个jQuery脚本,它将ajax发送到(静态表单提交)。他们的医生说要做到这一点:

$.ajax({
  url: "//formspree.io/you@email.com", 
  method: "POST",
  data: {message: "hello!"},
  dataType: "json" 
});
data: 'first=hgf&last=ghfgh',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
在我自己的jQuery脚本中,我有一个同样有效的脚本:

jQuery.ajax({
  type: "POST",
  url: "http://formspree.io/email@example.org",
  data: 'name=' + name + '&_replyto=' + email + '&message=' + message;,
  dataType: "json",
 });
然而,我在这方面的努力失败了——formspree声称我提交的是一份空表格

$http({
  url: "http://formspree.io/email@example.org",
  data: {
    email: email,
    message: message
  },
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  }

$http({
  url: "http://formspree.io/email@example.org",
  data: 'email=...&message=...',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  }
我能够做到这一点:

$.ajax({
  url: "//formspree.io/you@email.com", 
  method: "POST",
  data: {message: "hello!"},
  dataType: "json" 
});
data: 'first=hgf&last=ghfgh',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
但这会让FormSpree认为我不是ajax,并试图加载一个感谢页面。我不清楚这是否是原因。我收到了表格,但它在感谢页面上出现了错误


想法?

您应该以URL编码格式发送数据:

$http({
    url: "http://formspree.io/email@example.org",
    data: $.param({
        email: email,
        message: message
    }),
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
});

好的,似乎您还需要发送
Accept:application/json
头。如果指定
数据类型:“json”
,jQuery就会发送它。