2017-04-05 99 views
0

我知道這已經在其他地方的某種程度上得到了回答,但我嘗試過的任何東西似乎都無能爲力。我從來沒有用過jQuery(我總是有類似Angular的$ http來處理這個),但我得到一個Object對象,它應該吐出電子郵件信息。我已經包含下面的代碼。jQuery Post在提交時返回對象對象

var email = $("#email").val(); 
    $.post("http://httpbin.org/post", email, function(email, status){ 
     console.log("Email: " + email + "\nStatus: " + status); 
    }); 

我在做什麼錯?

+0

'的console.log(電子郵件)' – epascarello

+0

嘗試'功能(數據){執行console.log( '電子郵件:' + data.email)}'因爲你的成功方法。 – PHPglue

回答

1

感謝您的所有內容。它幫助我得出答案。那就是:

 $.post("http://httpbin.org/post", { email: email } , function(data, status){ 
     console.log(data.form.email); 
     }); 
0

你可能想透過JSON.stringify()爲對象,試圖爲這個返回的字符串表示這是你想要

0

可以更換加+用逗號,什麼!

var email = $("#email").val(); $.post("http://httpbin.org/post", email, function(email, status){ console.log("Email: ", email, "Status: ", status); });

希望這可以幫助你!

0

$.post()語法是:

$.post("ajax/test.html", function(response) { 
    // here response contains the server response in it 
}); 

在你的情況,如果它顯示Object Object,表示響應是JSON object,所以你要分析它喜歡:

$.post("ajax/test.html", function(response) { 
    var data = JSON.parse(response); 
    console.log(data.email); 
    console.log(data.status); 
}); 
0

$ .post是$的簡寫.ajax

$.ajax({ 
    type: "POST", 
    url: url, 
    data: data, 
    success: success, 
    dataType: dataType 
}); 

如果添加的console.log(電子郵件),它會顯示在對象的電子郵件

var email = $("#email").val(); 
$.post("https://httpbin.org/post", email, function(email, status){ 
    console.log("Email: " + email + "\nStatus: " + status); 
    console.log(email); 
}); 

enter image description here

https://api.jquery.com/jquery.post/