php - 从通过 AJAX 检索的对象设置文本字段值

标签 php jquery mysql json ajax

我有一个这样的对象,我通过 AJAX 请求检索它:

{
    "id": "1",
    "name": "Eyes"
} 

如何获取文本字段 #id_attributes#name_attributes 的数据?单击按钮时我尝试了此操作,但它给了我未定义/空白?

$.ajax({
    url: "to my json",
    cache: false,
    type: "POST",
    data: 'id=' + id,
    success: function (result) {              
        $("#id_attributes").val(result.id);
        $("#name_attributes").val(result.name);
    }
});

谁能帮帮我?

最佳答案

尝试:

$.ajax({
    url: "to my json",
    cache: false,
    type: "POST",
    dataType : 'json'
    data:{id:id},
    success: function (result) {              
        $("#id_attributes").val(result.id);
        $("#name_attributes").val(result.name);
    }
});

或者解析结果

success: function (result) {  
            var  jsonresult = JSON.parse(result);             
            $("#id_attributes").val(jsonresult.id);
            $("#name_attributes").val(jsonresult.name);
        }

关于php - 从通过 AJAX 检索的对象设置文本字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34196453/

相关文章:

javascript - 将数据从内容可编辑 div 复制到隐藏输入以进行表单提交?

php - 为什么 CakePHP 中的创建函数试图保存以前的条目?导致重复输入错误

php - 仅在登录时的 wordpress PHP 代码

php - Cakephp 3.0单元测试错误

javascript - 滚动时滚动到div,div消失时滚动到顶部

jquery - 单击这个或那个,然后执行某些操作

php - 在很多行中将值增加 1

MySQL sort_buffer_size

php - ICalendar 与 Spatie/ICalendar-Generator 创建新 session 而不是在 Outlook 中更新

php - 使用基于设备宽度的 PHP 变量替换图像源值