Jquery Parse Json on ajax 成功

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17496348/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:12:48  来源:igfitidea点击:

Jquery Parse Json on ajax success

jqueryajaxjson

提问by user1606423

I trie to get an json in my jquery ajax successe: to worke but i dose not.......

我试图在我的 jquery ajax 成功中获得一个 json:工作,但我没有......

Thats what i tried to do:

这就是我试图做的:

$("#addcatform").submit(function() {
  var str = $(this).serialize();
  $.ajax({
    type: "POST",
    url: "ajax.php",
    data: str,
    success: function(data){
      var json_x = data;
      alert(json_x.firstName2);
      $('#result').html(json_x.firstName2); 
      $('#result2').html(json_x.b); 
    }
  }); 

  return false;
  event.preventDefault();
}); // submit end

the php echos this:

php 回应了这一点:

$arr = array ('firstName2'=>'hallo','b'=>2,'c'=>3,'d'=>4,'e'=>5);
echo json_encode($arr);

Whats wrong with this? Thanks for helping!!!!

这有什么问题?谢谢帮忙!!!!

回答by Mohammad Adil

You need to parse your json before using it,

你需要在使用它之前解析你的json,

You can add a dataType in your request - jQuery will parse your response json

您可以在请求中添加 dataType - jQuery 将解析您的响应 json

$.ajax({
    type: "POST",
    url: "ajax.php",
    dataType: 'json',

Or, you can parse it yourself -

或者,你可以自己解析——

success: function(data){
    var json_x = $.parseJSON(data);

回答by Indra

You could try this:

你可以试试这个:

var data=$.ajax({
        type: "POST",
        url: 'ajax.php',
        data: {
            data:str
        }, 
        async: false,
        dataType: 'json'
    });
    var msg= data.responseText;
    msg=jQuery.parseJSON(msg);

I usually send either an array or the message 'error' from my php page

我通常从我的 php 页面发送一个数组或消息“错误”

if(msg=='error')
{
/* do something */
}
else
// use the data

This works with jquery 1.6->1.8

这适用于 jquery 1.6->1.8

EDIT: Since jquery 1.8 async is deprecated. i would recommend this format :

编辑:由于不推荐使用 jquery 1.8 async。我会推荐这种格式:

$.ajax({
        type: "POST",
        url: 'ajax.php',
        data: {
            data:str
        }, 
        dataType: 'json',
     ).done(function(data) {
      // do something with the data
     })
   .fail(function() {
    // give appropriate message
    })

http://api.jquery.com/jquery.ajax/

http://api.jquery.com/jquery.ajax/

回答by Sumurai8

datais a string in your example. Use jQuery.getJSON().Edit: As you cannot do a POST-request with getJSON (d?h) use .ajaxwith the appropiate dataType instead.This will retrieve the data via ajax, and parses the resulting string as if it is JSON. Even with getJSON, the result will be an array (or an array like object, not sure). This has no methods or variables you can access with the dot-notation. You'll need to access it via data['a'].

data在您的示例中是一个字符串。使用jQuery.getJSON()编辑:因为您不能使用 getJSON (d?h) 执行 POST 请求,请改用.ajax适当的数据类型。这将通过 ajax 检索数据,并将结果字符串解析为 JSON。即使使用getJSON,结果也将是一个数组(或类似对象的数组,不确定)。这没有您可以使用点符号访问的方法或变量。您需要通过 访问它data['a']

$.ajax({
  dataType: "json",
  type: "POST",
  url: "ajax.php",
  data: str,
  success: function(data){
    var json_x = data;
    alert(json_x['firstName2']);
    $('#result').html(json_x['firstName2']);
    $('#result2').html(json_x['b']); 
  } 
});