[英]Cannot receive Rails response with Ajax
當我從phonegap(使用javascript中的ajax)發布到我的Rails服務器時,發布成功,但是服務器沒有響應,所以最終失敗了。 我不明白為什么我無法得到答復..
例如,這是我的注冊腳本(帶有ajax的javascript):
$('#sign-up-button').click(function(e) {
var str = $("#signUpForm").serialize();
$(".error").remove();
e.preventDefault();
$.ajax({url: "http://localhost:3000/api/users.json",
type: "POST",
data: str,
success: function(result, status) {
alert('success');
$.mobile.changePage( "welcome.html", { transition: "slide"} );
},
error: function(result) {
alert('error');
}
});
});
和我在Ruby方面的代碼在Rails方面:
# POST /users
# POST /users.json
def create
@user = User.new(params[:user])
respond_to do |format|
if @user.save
format.html { redirect_to @user, notice: 'User was successfully created.' }
format.json { render json: @user, status: :created }
else
format.html { render action: "new" }
format.json { render json: @user.errors, status: :unprocessable_entity }
end
end
end
在螢火蟲中,我有:消息201 Created,創建了用戶(我可以檢查它),但是我沒有任何響應,因此出現消息alert('error')...
非常感謝您的建議!
如果期望空響應(例如201),則需要在$.ajax
選項中指定dataType: 'text'
。 現在發生的事情是jQuery嘗試將響應解析為JSON,但由於沒有解析響應而失敗。
我剛剛停止嘗試使其與Firefox一起使用,並以X代碼啟動了它。 有效。
我想知道是否需要在服務器上工作才能從帖子中獲取響應,有人知道X代碼是否創建了服務器?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.