【问题标题】:Returning response data not working with html返回响应数据不适用于 html
【发布时间】:2019-02-15 08:34:07
【问题描述】:

您好,我正在尝试通过 ajax 在同一页面上获取数据。但没有成功。当我使用 html(result) 打印成功消息时,它不起作用。但是当我使用 .text(result) 它工作但返回 [对象],[对象] 这是我的 ajax 代码。

$(document).ready(function(){
    $('#frm_rechargeHistory_process').submit(function(){
        var username = $("#username").val();
        var dataString = 'username='+ username;
        if(username==''){
            alert("Please Fill All Fields");
        }else{
        // AJAX Code To Submit Form.
            $.ajax({
              type: "POST",
              url: "getfoundhistory",
              data: dataString,
              cache: false,
              success: function(result){
                  alert(result);
                  $('#txtHint').html(result);
              }
            });
        }
    return false;
    });
});

虽然浏览器网络响应完美

 [{"Balanceledger": {"id":"2","username":"rawat","amount":"100","reason":"wdeadeses","requestid":"0","approveid":"0","created_date":"2018-11-19 10:41:03"}}].

【问题讨论】:

  • 尝试在控制台中记录您的响应,然后查看您的响应数据
  • Kurtis@console 数据为@@@ 0:Balanceledger:{id:“2”,用户名:“rawat”,金额:“100”,原因:“wdeadeses”,requestid:“0” , …} proto: 对象

标签: jquery ajax


【解决方案1】:

从您发布的内容来看,响应采用 JSON 格式。这需要以 TEXT 或最好的 HTML 格式表示的方式重建

在替换 HTML 内容之前尝试这样的操作

var tmpData = JSON.parse(result);
var formattedJson = JSON.stringify(result, null, '\t');

【讨论】:

  • Ahmad Khundaqji@can you post exact code with question .. 我收到 JSON.parse(result) 错误。请让我知道确切的代码是什么
  • 然后尝试第二个或参考以下答案stackoverflow.com/a/9997087/5225589
  • Ahmad Khundaqji@data 这样返回 [ { "Balanceledger": { "id": "2", "username": "rawat", "amount": "100", "reason": “wdeadeses”、“requestid”:“0”、“approveid”:“0”、“created_date”:“2018-11-19 10:41:03”}] 但我只需要数据行而不是 json
  • Ahmad Khundaqji@你在吗?
  • var formattedJson = JSON.stringify(result, null, '\t');@this 有效,但返回 json 数组。虽然我只想打印数据库结果
猜你喜欢
  • 1970-01-01
  • 2021-04-18
  • 1970-01-01
  • 2021-12-02
  • 1970-01-01
  • 2020-02-06
  • 2021-11-17
  • 2019-01-09
  • 1970-01-01
相关资源
最近更新 更多