我有一个简单的ajax帖子到服务器..
$(".invite-team-members-submit-btn").click(function() {
$.post("invite_team_member", { token: $("#token").val(), email: $("#email").val(), team: $("#team").val() })
.done(function (responseText) {
responseText = jQuery.parseJSON(responseText);
alert(responseText.response);
})
.fail(function (data) { alert("ERROR: " + data); })
.then(function () { alert("Something should happen."); });
});
返回的JSON看起来像这样......
{"response":"Person has been invited."}
我在控制台中的响应标题看起来像这样......
Response Headers
Cache-Control max-age=0, private, must-revalidate
Connection close
Content-Type application/json; charset=utf-8
Date Wed, 22 May 2013 21:45:07 GMT
Etag "e5b5e12acbcc78372b2a861027b66c05"
Status 200 OK
Transfer-Encoding chunked
X-Request-Id d835ce021eff7733d67ebfcdd468bdf2
X-Runtime 0.007909
x-ua-compatible IE=Edge
在我的控制台中,我看到它是服务器并返回了相应的文本,但我的浏览器中没有收到警报。我只是出于对错误的想法。 jQuery是否更新了我缺少的东西?
答案 0 :(得分:6)
您错误地使用了$.post
。您的意思是使用$.ajax
并指定type: "post"
属性。
$.post
的第一个参数是URL。在你的情况下,它是一个对象。我认为也许jQuery最终会使用当前页面URL来发出请求(这就是为什么你会看到),但我不能确定。您可以将其重写为:
$.post("invite_team_member", {data: data})
.done(function (responseText) { alert(responseText); })
.fail(function (data) { alert("ERROR: " + data); });
答案 1 :(得分:1)
使用$.ajax()
,然后将类型设置为"POST"
答案 2 :(得分:1)
试试这段代码:
mydata = "name=Jon&location=USA";
$.ajax({
type : 'POST',
url : 'myphp.php',
data : mydata
}).done(function(msg){
alert("DONE");
});