gpt4 book ai didi

javascript - $.post(url, 函数(数据,状态) {警报(数据)});警报()不工作

转载 作者:行者123 更新时间:2023-12-03 11:45:11 29 4
gpt4 key购买 nike

我有一个 Rails 应用程序,它与其他 Rails 应用程序通信以进行数据插入。我使用 jQuery $.post 方法进行数据插入。对于插入,我的其他 Rails 应用程序显示 200 OK。但在post方法的函数alert中没有显示任何内容。

jQuery 代码:

$.post("http://localhost:3000/persons.json",
{
person:
{
id: $("#id").val(),
name: $("#name").val(),
}
},function(data, status){
alert("Hello World!!");
alert("Data: " + data + "\nStatus: " + status);
});

其他 Rails 应用程序中的 Controller 代码:

p = Person.new(a_params)
if p.save
notice = "Person data entered successfully"
render json: notice, status: 200
end

我哪里出错了?

最佳答案

您没有返回正确的 JSON 对象作为响应,因此 jQuery 将响应解释为错误,而不是成功

您可能想尝试一下:

p = Person.new(a_params)
if p.save
notice = "Person data entered successfully"
render json: {notice: notice, status: 200} # This ruby Hash is returned as a JSON object
end

然后在 jQuery 代码中,您可以访问 data['notice']data['status']

关于javascript - $.post(url, 函数(数据,状态) {警报(数据)});警报()不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087573/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com