0

I have a small and likely silly oversight on a piece of script that was actually working a few weeks ago.

$( document ).ready(function() {
    $( "#subscribe-btn" ).click(function() {
        var email = $( "#subscribe-input" ).val();
        if(email != ""){
            $.ajax({
                type: "POST",
                url: "/subscribers",
                data: {
                  email : email
                },
                dataType: "html"
            });
        }
        else {
            alert("Email field is empty!");
        };
    });
});

When the button is clicked, it returns the error 400 (Bad Request). How to fix this error?

Ram
  • 3,092
  • 10
  • 40
  • 56
acacia
  • 1,375
  • 1
  • 14
  • 40
  • 4
    Have you looked in the server logs? What are you seeing there? Is there a stack trace you can share? – Alex Lynham Jun 17 '15 at 12:57
  • @AlexLynham, your spot on! – acacia Jun 17 '15 at 12:59
  • Possibly a bad contentType. By default, $.ajax() uses `'application/x-www-form-urlencoded; charset=UTF-8'`. Perhaps you need to use something else? `contentType: application/json`? – Mike Nitchie Jun 17 '15 at 13:21
  • [This SO thread](http://stackoverflow.com/questions/19671317/400-bad-request-http-error-code-meaning) may be helpful to you. – Ravimallya Jun 18 '15 at 07:46
  • Improved the title to better reflect your question. As @AlexLynham mentions please add more details like stack trace or server logs. Add any changes that were made after which the code started giving the error. – Ram Jun 19 '15 at 18:52
  • Without understanding what data the server-side script is expecting, it is impossible to tell you why the AJAX call would return 400 error. – Mike Brant Jun 19 '15 at 18:55
  • what is /subscribers? That doesn't look valid. – jordaniac89 Jun 19 '15 at 19:15
  • @jordaniac89, its fine. its a url from the application – acacia Jun 19 '15 at 20:22

0 Answers0