dongyi6543 2016-02-26 18:50
浏览 281
已采纳

从json字符串中提取数据元素

I am using the following script to my php script on my server;

$.ajax({
            type: 'POST',
            url: 'http://xx.php',
            data: { anyNumber: val1 },
            success: function(data) {
                alert(data);
            }
});

the php script correct queries sqlsrv database and returns row of data that I encode with echo json_encode($rows); I then correctly receive the json string into data (as above example) When I display the data with alert(data); as above, I get the following:

"({\"id\":1,\"Username\":null,\"Category\":null,\"VIN\":null,\"Make\":null,\"Model\":null,\"Colour\":null,\"Registration\":\"12345     \",\"RegisterNumber\":null,\"EngineNo\":null,\"Branding\":null,\"Remarks\":null,\"Photo1\":null})"

I have tried various methods but cannot assign the various elements to variables. Please help

  • 写回答

1条回答 默认 最新

  • dqd54099 2016-02-26 18:55
    关注

    just set the dataType of your $.ajax call to json:

    $.ajax({
            type: 'POST',
            url: 'http://xx.php',
            dataType: 'json',
            data: { anyNumber: val1 },
            success: function(data) {
                alert(data);
            }
    });
    

    explanation: when delivering data with GET or POST a lot of characters are getting masked. I think your $.ajax call just doesn't expect JSON as an answer but text or html. that's why it's not reading the JSON string correctly and keeping the masking of the characters.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于博途V17进行仿真时无法建立连接问题
  • ¥15 请问下这个红框里面是什么文档或者记事本编辑器
  • ¥15 机器学习教材中的例题询问
  • ¥15 求.net core 几款免费的pdf编辑器
  • ¥15 为什么安装HCL 和virtualbox之后没有找到VirtualBoxHost-OnlyNetWork?
  • ¥15 C# P/Invoke的效率问题
  • ¥20 thinkphp适配人大金仓问题
  • ¥20 Oracle替换.dbf文件后无法连接,如何解决?(相关搜索:数据库|死循环)
  • ¥15 数据库数据成问号了,前台查询正常,数据库查询是?号
  • ¥15 算法使用了tf-idf,用手肘图确定k值确定不了,第四轮廓系数又太小才有0.006088746097507285,如何解决?(相关搜索:数据处理)