JSON返回为未定义 [英] JSON returning as undefined

查看:103
本文介绍了JSON返回为未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有一个提交表单,并发送和邮件,如果邮件被成功提交,我EN codeA PHP数组看起来像这样一个Ajax请求,

$成功=阵列(状态=>中邮件发送);

我再检查我的Ajax请求的数据的状态,看看状态匹配邮件已发送但是当我警报(数据)我得到不确定的,我究竟做错了什么?下面是我的javascript,

  $。阿贾克斯({
                    网址:< PHP的回声BASE_URL();>家用/回调?,
                    数据:$(#回调)序列化()。
                    键入:POST,
                    数据类型:JSON,
                    成功:功能(数据){
                        $(#的fancybox内容的div)HTML(数据);
                        警报(data.state);
                    }
                });
 

解决方案

使用

  json_de code($成功,真正的);
 

  

为真时,返回的对象将被转换成关联数组。

Hello I have an ajax request that submits a form and sends and email, if the email is submitted successfully, I encode a PHP array that looks like this,

$success = array("state" => "Email Sent");

I am then checking the state of data in my ajax request to see if state matches "Email Sent" however when I alert(data) i get undefined, what am I doing wrong? Below is my javascript,

$.ajax({
                    url: "<?php echo base_url(); ?>home/callback",
                    data: $("#callback").serialize(),
                    type: "POST",
                    dataType: "JSON",
                    success: function(data){
                        $("#fancybox-content div").html(data);
                        alert(data.state);
                    }
                });

解决方案

use

json_decode($success, true); 

When TRUE, returned objects will be converted into associative arrays.

这篇关于JSON返回为未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆