javascript - 调试 jQuery AJAX 响应 : what I'm doing wrong?

标签 javascript jquery ajax debugging json

$.ajax({
    type: 'POST',
    url: 'place/add',
    data: {
        lat: lat,
        lng: lng,
        name: name,
        address: address,
        phone: phone,
        review: review,
        category: category
    },
    success: function(data) {
    alert(data);
    alert(data.id);
    // ......
});

第一个警报给出:{"id":"2","success":true},但第二个警报给出:undefined

最佳答案

您需要将预期返回的数据类型指定为 JSON:

$.ajax({
    type: 'POST',
    dataType: 'json', // specifies the return type
    url: 'place/add',
    data: {
        lat: lat,
        lng: lng,
        name: name,
        address: address,
        phone: phone,
        review: review,
        category: category
    },
    success: function(data) {
        alert(data);
        alert(data.id);
        // ......
    }
});

关于javascript - 调试 jQuery AJAX 响应 : what I'm doing wrong?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1696271/

相关文章:

javascript - 在数据库中同时搜索多个表

javascript - HTML/JavaScript 拖放 - 是否可以使重影图像 *不* 看起来 'washed out' ?

jquery - Bootstrap : Mouse-enter animation issue with img-responsive [Opera only]

php - Ajax更新、比较和插入数据到mysql

php - 如何使用ajax在地址栏中显示选定的表格行id

javascript - $scope.$watch 使用 Angular 和 Ionic/Cordova 的 div 标签

javascript - JavaScript 中的 HTML 复选框开/关检测

javascript - 克隆主干模型属性

Javascript 将 onload 附加到弹出窗口

javascript - 等待 ajax 返回的正确方法(我不想使用成功处理程序。)