0

我是 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();**
                    }
            }
        ]
    }
});
4

2 回答 2

0

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

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

header('Cache-Control: no-cache, must-revalidate');
header("content-type:application/json");
于 2012-09-27T12:07:56.653 回答
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\"}";
?>
于 2012-09-27T03:25:24.060 回答