Sencha Touch:从Sencha Touch示例调用php函数会导致无效的JSON字符串错误。

时间:2012-09-25 21:07:46

标签: php javascript json extjs

我是Sencha Touch的新手。我按照Sencha Touch的“入门”示例视频(http://docs.sencha.com/touch/2-0/#!/guide/getting_started)。在示例中,有一个联系表单(代码如下)调用视频中未定义的php函数,导致找不到文件控制台错误。当我添加一个PHP函数时,我得到以下控制台错误:

“未捕获错误:您正在尝试解码无效的JSON字符串:”

即使文件为空,我也会收到此错误。有谁知道我做错了什么?或者任何教程解释如何从Sencha调用php函数?

联系表单代码,用于查看如何调用php函数:

Ext.define('GS.view.Contact', {
    extend:  'Ext.form.Panel',
    xtype:  'contactform',

    requires:  [
        'Ext.form.FieldSet',
        'Ext.field.Email'
        ],

    config:  {
        title:  'Contact',
        iconCls:  'user',
        **url:  'php/Contact.php',**

        items:  [
            {
                xtype:  'fieldset',
                title:  'Contact Us',
                instructions:  '(email is not required)',
                items:  [
                    {
                        xtype:  'textfield',
                        name:  'name',
                        label:  'Name'
                    },
                    {
                        xtype:  'emailfield',
                        name:  'email',
                        label:  'Email'
                    },
                    {
                        xtype:  'textareafield',
                        name:  'message',
                        label:  'Message'
                    }
                ]

            },

            {
                xtype:  'button',
                text:  'Send',
                ui:  'confirm',
                **handler:  function(){
                    this.up('contactform').submit();**
                    }
            }
        ]
    }
});

2 个答案:

答案 0 :(得分:0)

该错误非常自我解释。您调用的PHP脚本的输出格式不正确JSON。例如,您需要在HTTP响应上放置正确的JSON标头,还需要确保实际数据是JSON。

你可以尝试用这样的东西替换你的PHP文件,看看你是否可以通过错误?

<?php
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo "{\"success\":true, \"message\":\"Hello World\"}";
?>

答案 1 :(得分:0)

错误表明收到的JSON无效。确保PHP页面创建有效的JSON。您可以从简单输出您知道有效JSON的硬编码字符串开始。

此外,请确保页面使用以下标题向浏览器发送正确的内容类型:

header('Cache-Control: no-cache, must-revalidate');
header("content-type:application/json");