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

从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

图片转代码服务由CSDN问答提供 功能建议

我在服务器上的php脚本中使用以下脚本;

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

php脚本正确查询sqlsrv数据库并返回我用 echo编码的数据行 json_encode($ rows); 然后正确地将json字符串接收到数据中(如上例所示) 当我用 alert(data); 显示数据时,我得到以下内容:

 <代码> “({\” ID \ “:1,\” 用户名\ “:空,\” 类别\ “:空,\” VIN \ “:空,\” 使 \“:null,\”Model \“:null,\”Color \“:null,\”Registration \“:\”12345 \“,\”RegisterNumber \“:null,\”EngineNo \“:null,\  “Branding”:null,\“Remarks \”:null,\“Photo1 \”:null})“
   
 
 

我尝试了各种方法,但无法分配 变量的各种元素。 请帮助

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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.

    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题