PDA

View Full Version : Simple Login-Form, Error: "'You're trying to decode an invalid JSON String..."



suamikim
15 Mar 2012, 12:39 AM
Hi there,

i'm pretty new to ExtJs and JavaScript in general and i'm trying to build a pretty simple login-form. Unfortunately i'm stuck right from the beginning.

I've built a small login-form like this:

login.js

Ext.onReady(function() {
Ext.create('Ext.form.Panel', {
title: 'Please Login',
bodyPadding: 5,
width: 350,

// The form will submit an AJAX request to this URL when submitted
url: 'php/login.php',

// Fields will be arranged vertically, stretched to full width
layout: 'anchor',
defaults: {
anchor: '100%'
},

// The fields
defaultType: 'textfield',
items: [{
fieldLabel: 'Username',
name: 'loginUsername',
allowBlank: false
},{
fieldLabel: 'Password',
name: 'loginPassword',
inputType: 'password',
allowBlank: false
}],

// Login button
buttons: [{
text: 'Login',
formBind: true, //only enabled once the form is valid
disabled: false,
handler: function() {
var form = this.up('form').getForm();
if (form.isValid()) {
form.submit({
success: function(form, action) {
Ext.Msg.alert('Success', action.result.msg);
},
failure: function(form, action) {
Ext.Msg.alert('Failed', action.result.msg);
}
});
}
}
}],
renderTo: Ext.getBody()
});
});

Basically i've just copy & pasted the first example from the official ext-documentation (http://docs.sencha.com/ext-js/4-0/#!/api/Ext.form.Panel (http://docs.sencha.com/ext-js/4-0/#%21/api/Ext.form.Panel)) and made some small changes...

The corresponding php-code (php/login.php):

<?php
echo '{success: true, msg: "Login successfull!"}';
?>;

As you can see i'm only returning a static JSON-string from this file for the pure purpose of testing.

Unfortunately i'm receiving the following error-message in firebug when i try to submit anything:

'You're trying to decode an invalid JSON String: {success: true, msg: "Login successfull!"};' when calling method: [nsIDOMEventListener::handleEvent]

When i try to manually decode the same string with

Ext.JSON.decode('{success: true, msg: "Login successfull!"}');
it executes just fine and creates a valid JSON-object...

What am i missing?

Thanks

Erikvip
15 Mar 2012, 12:54 AM
In php, try using json_encode instead of handling the JSON manually.

something like:



<?php
echo json_encode(Array('success' => true, 'message' => 'Login Successful'));

suamikim
15 Mar 2012, 1:18 AM
Already tried with the json_encode-function which gives me the same result with a slightly different received JSON-string in the error-message (only differs in the way the apostrophs are set):


'You're trying to decode an invalid JSON String: {"success":true,"msg":"Login Successful"};' when calling method: [nsIDOMEventListener::handleEvent]

suamikim
15 Mar 2012, 2:56 AM
I just found the (quiet annoying) error myself:

The only problem was the trailing semicolon at the very end of the php-file:

<?php
echo '{success: true, msg: "Login successfull!"}';
?>;

Without the semicolon everything is working as expected:

<?php
echo '{success: true, msg: "Login successfull!"}';
?>

p.s.: How can i mark this thread as answered?