这个简单的jquery/json函数缺少什么?

这个简单的jquery/json函数缺少什么?,jquery,json,Jquery,Json,我对一个文件进行了一个简单的ajax查询,基本上返回JSON error或success 即: 然而,问题是,即使我从我的ajax帖子中得到这个json数据返回,我似乎根本无法访问数据 我的jquery如下所示: $.ajax({ type: 'POST', url: 'process_sale.php', data: $(this).serialize(), cache: false,

我对一个文件进行了一个简单的ajax查询,基本上返回JSON error或success

即:

然而,问题是,即使我从我的ajax帖子中得到这个json数据返回,我似乎根本无法访问数据

我的jquery如下所示:

     $.ajax({
          type: 'POST',
          url: 'process_sale.php',
          data: $(this).serialize(), 
          cache: false,
          dataType: 'json',
          success: function(data) {

            if(data['success']=='1'){

                alert('hi');

                $('#feedback').html('<strong>Congratulations</strong');

            }

            if(data['error']=='1') {

                alert('hi');

                $('#feedback').html(data['error_msg']);


            }

                   // this following alert does nothing, because its empty even if the json 
                   // returns what I pasted above example result.

                   alert(data['error'] + ' ' + data['success']);

          }
        });
$.ajax({
键入:“POST”,
url:“process_sale.php”,
数据:$(this).serialize(),
cache:false,
数据类型:“json”,
成功:功能(数据){
如果(数据['success']='1'){
警报(“hi”);

$(“#feedback”).html(“恭喜你你试图解析无效的JSON——你的示例中有两个相邻的JSON对象。你需要将它们放在一个数组中,或者如果你想让JS理解它们,就将它们设为一个对象:

// You want this:
{"error":"1", // note , not {}
 "error_msg":" Invalid Expiry Date. "+
              "Your credit card has not been billed for this transaction."}

// or this (notice the `,` and the `[`, and `]`)
[{"error":"1"},{"error_msg":" Invalid Expiry Date. Your credit card has not been billed for this transaction."}]

根据您的其余代码,我敢打赌第一个将更好地满足您的需要。您可能更喜欢访问错误消息数据['error'],而不是数据[0]['error']

您试图解析无效的JSON——您的示例中有两个相邻的JSON对象。您需要将它们放在一个数组中,或者如果希望JS理解它们,则将它们设为一个对象:

// You want this:
{"error":"1", // note , not {}
 "error_msg":" Invalid Expiry Date. "+
              "Your credit card has not been billed for this transaction."}

// or this (notice the `,` and the `[`, and `]`)
[{"error":"1"},{"error_msg":" Invalid Expiry Date. Your credit card has not been billed for this transaction."}]

根据您的其余代码,我敢打赌第一个会更好地满足您的需要。您可能更喜欢访问错误消息数据['error'],而不是数据[0]['error']

json数据应该是这样的:

{"error":"1","message":"This is an error message"}
Ajax调用:

$.ajax({
          type: 'POST',
          url: 'process_sale.php',
          data: $(this).serialize(), 
          cache: false,
          dataType: 'json',
          success: function(data) {
            if (data != null){
            if(data.error=='0'){

                alert('Success');

                $('#feedback').html('<strong>Congratulations</strong');

            }

            if(data.error=='1') {

                alert('Error occurred');

                $('#feedback').html(data.mesage);


            }
           }else{
                //do something with NULL
           } 

          }
        });
$.ajax({
键入:“POST”,
url:“process_sale.php”,
数据:$(this).serialize(),
cache:false,
数据类型:“json”,
成功:功能(数据){
如果(数据!=null){
如果(data.error='0'){
警惕(“成功”);

$('#feedback').html('恭喜json数据应该是这样的:

{"error":"1","message":"This is an error message"}
Ajax调用:

$.ajax({
          type: 'POST',
          url: 'process_sale.php',
          data: $(this).serialize(), 
          cache: false,
          dataType: 'json',
          success: function(data) {
            if (data != null){
            if(data.error=='0'){

                alert('Success');

                $('#feedback').html('<strong>Congratulations</strong');

            }

            if(data.error=='1') {

                alert('Error occurred');

                $('#feedback').html(data.mesage);


            }
           }else{
                //do something with NULL
           } 

          }
        });
$.ajax({
键入:“POST”,
url:“process_sale.php”,
数据:$(this).serialize(),
cache:false,
数据类型:“json”,
成功:功能(数据){
如果(数据!=null){
如果(data.error='0'){
警惕(“成功”);

$(“#feedback”).html(“祝贺AJAX响应是否也返回错误(非2xx)状态?如果是,则成功回调不会捕获它。AJAX响应是否也返回错误(非2xx)状态?如果是,则成功回调不会捕获它。