使用httpbin将表单数据检索为json

时间:2015-03-10 01:21:41

标签: jquery ajax json

我想通过对httpbin.org进行ajax调用来检索我的html表单数据作为json,但由于某种原因我没有得到我想要的。

$("#contact").submit(
        $.ajax({
            url: "http://httpbin.org/post",
            type:'POST',
            dataType: 'json',
            data: JSON.stringify({
                "fullname": $('#fullname').val(), 
                "email": $('#email').val()
            })

        }).done(function(data){
            console.log(data);

        })
)

我希望看到它像这样返回json数据:(来自http://httpbin.org/forms/post

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "comments": "", 
    "custemail": "fffadsfs@gmail.com", 
    "custname": "dsfdasfads", 
    "custtel": "12312312", 
    "delivery": "11:15", 
    "size": "small", 
    "topping": "bacon"
  }, 
  "headers": {
    "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 
    "Accept-Encoding": "gzip, deflate", 
    "Accept-Language": "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2", 
    "Cache-Control": "max-age=0", 
    "Content-Length": "119", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Cookie": "_gat=1; _ga=GA1.2.1002093679.1425610085", 
    "Host": "httpbin.org", 
    "Origin": "http://httpbin.org", 
    "Referer": "http://httpbin.org/forms/post", 
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.76 Safari/537.36"
  }, 
  "json": null, 
  "origin": "174.51.135.148", 
  "url": "http://httpbin.org/post"
}

1 个答案:

答案 0 :(得分:0)

您无法在控制台中看到数据,因为您在event.preventDefault();方法后缺少done。 因此,在表单提交后重新加载页面。

更多信息: https://api.jquery.com/submit/