2

I have currently a jQuery script that sends ajax to http://formspree.io (static form submission). Their docs say to do this:

$.ajax({
  url: "//formspree.io/[email protected]", 
  method: "POST",
  data: {message: "hello!"},
  dataType: "json" 
});

In my own jQuery script - I have this which also works:

jQuery.ajax({
  type: "POST",
  url: "http://formspree.io/[email protected]",
  data: 'name=' + name + '&_replyto=' + email + '&message=' + message;,
  dataType: "json",
 });

However my efforts to achieve this in Angular have failed - formspree claims I am submiting an empty form.

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

$http({
  url: "http://formspree.io/[email protected]",
  data: 'email=...&message=...',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  }

I am able to do this:

data: 'first=hgf&last=ghfgh',
headers: {'Content-Type': 'application/x-www-form-urlencoded'},

But this causes FormSpree to think I am a non ajax I think and tries to load a thank you page. I am unclear if this is tbe reason though. I get the form but it throws errors about the thank you page.

Ideas?

1 Answer 1

2

You should send data in form-urlencoded format:

$http({
    url: "http://formspree.io/[email protected]",
    data: $.param({
        email: email,
        message: message
    }),
    method: 'POST',
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/x-www-form-urlencoded'
    }
});
1
  • Ok, seems that you also need to send Accept: application/json header. jQuery sends it if you specify dataType: 'json'.
    – dfsq
    Jul 28, 2015 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.