Results 1 to 2 of 2

Thread: Getting [E] Ext.JSON.decode(): You're trying to decode an invalid JSON String

  1. #1
    Sencha User
    Join Date
    Mar 2015
    Posts
    1
    Vote Rating
    -1
      -1  

    Default Getting [E] Ext.JSON.decode(): You're trying to decode an invalid JSON String

    Hello Everyone,<br>
    I'm new to extjs from past 1 week i'm working on integrating extjs with php and mysql for using mysql database in my project and making crud operations i'm successfull in creating, deleting, and reading the records from database. But i'm getting this error while updating record "<strong>[E] Ext.JSON.decode(): You're trying to decode an invalid JSON String: Record updated successfully</strong>"<br>
    <br>
    Here is my extjs code,<br>
    <br>
    function for updating record in MainController.js:<br>
    <br>
    validityCheck: function( me, field, isValid, eOpts, md1){<br>
    debugger;<br>
    var selectedDay = "Monday";<br>
    <br>
    <br>
    var st = Ext.data.StoreManager.lookup('MyApp.store.LimitsStore'),<br>
    stLen = st.getData().length;<br>
    var timeLabel = "8:30 AM";<br>
    for(var i=0; i&lt;=stLen-1; i++){<br>
    var rec = st.getAt(i);<br>
    if(rec.data.day == selectedDay){<br>
    if(rec.data.timelimit == timeLabel){<br>
    var offLimit = rec.data.daylimit, // value here im getting is 20<br>
    resultLimit = offLimit-1,<br>
    limitId = rec.data.id; //value here im getting is 1<br>
    var msg = 'More '+resultLimit+' left';<br>
    Ext.toast({<br>
    html: msg,<br>
    title: 'INFO',<br>
    width: 200,<br>
    align: 't'<br>
    });<br>
    <br>
    <br>
    var values = {};<br>
    values['id'] = limitId;<br>
    values['day'] = selectedDay;<br>
    values['timelimit'] = timeLabel;<br>
    values['daylimit'] = resultLimit;<br>
    rec.set(values);<br>
    st.sync();<br>
    }<br>
    }<br>
    }<br>
    }<br>
    <br>
    LimitsStore.js:<br>
    <br>
    Ext.define('MyApp.store.LimitsStore', {<br>
    extend: 'Ext.data.Store',<br>
    model: 'MyApp.model.LimitsModel',<br>
    autoLoad: true,<br>
    //pageSize: 5, // numero de registros por Grid<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;storeId: 'limitsstore',<br>
    proxy: {<br>
    type: 'ajax',<br>
    api: {<br>
    read: 'data/readLimits.php',<br>
    update: 'data/updateUser.php',<br>
    create: 'data/createUser.php',<br>
    destroy: 'data/deleteUser.php'<br>
    },<br>
    reader: {<br>
    type: 'json',<br>
    rootProperty: 'data',<br>
    successProperty: 'success'<br>
    },<br>
    writer: {<br>
    type: 'json',<br>
    rootProperty: 'data',<br>
    encode: true<br>
    }<br>
    }<br>
    });<br>
    <br>
    <br>
    LimitsModel.js:<br>
    <br>
    Ext.define('MyApp.model.LimitsModel', {<br>
    extend: 'Ext.data.Model',<br>
    idProperty: 'id',<br>
    fields: [{name:'id',type: 'int'}, {name:'day',type: 'string'}, {name:'timelimit',type: 'string'}, {name:'daylimit',type: 'int'}]<br>
    });<br>
    <br>
    updateUser.php:<br>
    <br>
    &lt;?php<br>
    /*<br>
    * To change this template, choose Tools | Templates<br>
    * and open the template in the editor.<br>
    */<br>
    require_once 'lib/response.php';<br>
    require_once 'lib/request.php';<br>
    <br>
    <br>
    <br>
    <br>
    class User<br>
    {<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var $id;<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var $day;<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var $timelimit;<br>
    var $daylimit;<br>
    }<br>
    <br>
    <br>
    require_once('database_connection.php');<br>
    $request = new Request(array());<br>
    <br>
    <br>
    if(isset($request-&gt;params))<br>
    {<br>
    <br>
    <br>
    $array_r=$request-&gt;params;<br>
    $user= new User();<br>
    $user-&gt;id=($array_r-&gt;id);<br>
    $user-&gt;day=($array_r-&gt;day);<br>
    $user-&gt;timelimit=($array_r-&gt;timelimit);<br>
    $user-&gt;daylimit=($array_r-&gt;daylimit);<br>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo $user-&gt;day;<br>
    <br>
    <br>
    //update<br>
    $query="UPDATE limits SET day='$user-&gt;day', timelimit='$user-&gt;timelimit', daylimit=$user-&gt;daylimit WHERE id=$user-&gt;id";<br>
    $result = mysql_query($query);<br>
    //object response<br>
    $res = new Response();<br>
    $res-&gt;success = true;<br>
    $res-&gt;message = "Updated record";<br>
    <br>
    <br>
    <br>
    <br>
    /////Query Last update TO validate data was updated<br>
    $result = mysql_query("SELECT * FROM limits where id=$user-&gt;id");<br>
    $query_array=array();<br>
    $i=0;<br>
    //Iterate all Select<br>
    while($row = mysql_fetch_array($result))<br>
    {<br>
    //Create New User instance<br>
    $user = new User();<br>
    //Fetch User Info<br>
    $user-&gt;id=$row['id'];<br>
    $user-&gt;day=$row['day'];<br>
    $user-&gt;timelimit=$row['timelimit'];<br>
    $user-&gt;daylimit=$row['daylimit'];<br>
    <br>
    <br>
    //Add User to ARRAY<br>
    $query_array[$i]=$user;<br>
    $i++;<br>
    }<br>
    <br>
    <br>
    ///////////<br>
    <br>
    <br>
    <br>
    <br>
    $res-&gt;data = $query_array;<br>
    print_r($res-&gt;to_json());<br>
    }<br>
    else<br>
    {<br>
    //echo "false";<br>
    $res = new Response();<br>
    $res-&gt;success = false;<br>
    $res-&gt;message = "Error Create record";<br>
    $res-&gt;data = array();<br>
    print_r($res-&gt;to_json());<br>
    }<br>
    ?&gt;<br>
    <br>
    response.php:<br>
    <br>
    &lt;?php<br>
    /**<br>
    * @class Response<br>
    * A simple JSON Response class.<br>
    */<br>
    class Response {<br>
    public $success, $data, $total,$message, $errors, $tid, $trace;<br>
    <br>
    <br>
    public function __construct($params = array()) {<br>
    $this-&gt;success = isset($params["success"]) ? $params["success"] : false;<br>
    $this-&gt;message = isset($params["message"]) ? $params["message"] : '';<br>
    $this-&gt;total = isset($params["total"]) ? $params["total"] : '';<br>
    $this-&gt;data = isset($params["data"]) ? $params["data"] : array();<br>
    }<br>
    <br>
    <br>
    public function to_json() {<br>
    return json_encode(array(<br>
    'success' =&gt; $this-&gt;success,<br>
    'message' =&gt; $this-&gt;message,<br>
    'total' =&gt; $this-&gt;total,<br>
    'data' =&gt; $this-&gt;data<br>
    ));<br>
    }<br>
    }<br>
    <br>
    <br>
    request.php:<br>
    <br>
    &lt;?php<br>
    /* <br>
    * To change this template, choose Tools | Templates<br>
    * and open the template in the editor.<br>
    */<br>
    class Request {<br>
    public $method, $params;<br>
    <br>
    <br>
    public function __construct($params) {<br>
    // $this-&gt;restful = (isset($params["restful"])) ? $params["restful"] : false;<br>
    $this-&gt;method = $_SERVER["REQUEST_METHOD"];<br>
    $this-&gt;parseRequest();<br>
    }<br>
    <br>
    <br>
    protected function parseRequest() {<br>
    <br>
    <br>
    // grab JSON data if there...<br>
    $this-&gt;params = (isset($_REQUEST['data'])) ? json_decode(stripslashes($_REQUEST['data'])) : null;<br>
    <br>
    <br>
    if (isset($_REQUEST['data'])) {<br>
    $this-&gt;params = json_decode(stripslashes($_REQUEST['data']));<br>
    } else {<br>
    $raw = '';<br>
    $httpContent = fopen('php://input', 'r');<br>
    //print_r($httpContent);<br>
    while ($kb = fread($httpContent, 1024)) {<br>
    $raw .= $kb;<br>
    }<br>
    $params = json_decode(stripslashes($raw));<br>
    if ($params) {<br>
    $this-&gt;params = $params-&gt;data;<br>
    }<br>
    }<br>
    }<br>
    }<br>
    ?&gt;<br>
    <br>
    database-connection.php:<br>
    <br>
    &lt;?php<br>
    <br>
    <br>
    $con = mysql_connect("localhost","root","");<br>
    if (!$con)<br>
    {<br>
    die('Could not connect: ' . mysql_error());<br>
    }<br>
    <br>
    <br>
    mysql_select_db("usersdata", $con);<br>
    <br>
    <br>
    <br>
    <br>
    ?&gt;<br>
    <br>
    <img src="https://www.sencha.com/forum/attachment.php?attachmentid=53812&amp;stc=1" attachmentid="53812" alt="" id="vbattach_53812" class="previewthumb"><br>
    <br>
    <br>
    image of <strong>limits</strong> table in <strong>usersdata</strong> database<br>
    <br>
    please help me through this. it's very important for me please<br>
    Thanks.

  2. #2
    Sencha User
    Join Date
    Feb 2013
    Location
    California
    Posts
    11,985
    Answers
    506
    Vote Rating
    296
      0  

    Default

    I would suggest formatting your code to make it a little more readable so others can more easily help. It sounds like the JSON response from the server is not valid JSON. Can you post what your server is returning?

Similar Threads

  1. Replies: 2
    Last Post: 24 Nov 2014, 11:50 AM
  2. You're trying to decode an invalid JSON String
    By softball in forum Ext: Q&A
    Replies: 6
    Last Post: 22 Jun 2014, 4:23 PM
  3. You're trying to decode an invalid JSON String:
    By kuldeep.dave in forum Sencha Touch 2.x: Q&A
    Replies: 2
    Last Post: 13 Feb 2013, 8:50 PM
  4. Replies: 2
    Last Post: 9 Nov 2012, 1:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •