Results 1 to 7 of 7

Thread: JSONP callback error

  1. #1
    Sencha User
    Join Date
    Aug 2011
    Posts
    35

    Default JSONP callback error

    Hi all,

    I will make a jsonp data store, but when i load my application chrome says:
    Uncaught ReferenceError: getData is not defined

    Index.html
    Code:
    <!DOCTYPE html>
    <html>
    <head>
        <title>DemoApp</title>
        <link rel="stylesheet" href="touch2/resources/css/sencha-touch.css" type="text/css">
    
    
        <script type="text/javascript" src="touch2/sencha-touch-all-debug.js"></script>
        <script type="text/javascript" src="app.js"></script>
        <script src="http://www.domain.com/product.php?callback=getData"></script>
    </head>
    <body></body>
    </html>
    Store:
    Code:
    Ext.define('app.store.Search', 
    {    
        extend: 'Ext.data.Store',
        config: {
            model: 'app.model.search',
            proxy: 
            {
                type: 'jsonp',
                url: 'http://domain.com/product.php',
                callbackKey: 'callback',
                reader: 
                {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'total',
                    successProperty: 'success'
                },
                writer: 
                {
                    type: 'json'
                }            
            }
        }
    });
    product.php
    Code:
    <?php
        include_once('./../includes/connection.php');
        
        $callback = $_GET['callback'];
        
        $return = array(
            'success' => false,
            'data' => null
        );
        
        $sql = "QUERY";
        $result = Database::execute($sql);
        
        $return['data'] = $result;
        $return['total'] = count($result);
        $return['success'] = true;
        
        if ($callback) 
        {
            header('Content-Type: text/javascript');
            echo $callback . '(' . json_encode($return) . ');';
        } 
        else 
        {
            header('Content-Type: application/x-json');
            echo json_encode($return);
        }
        
    ?>
    JSON product.php:
    Code:
    getData({"success":true,"data":[{"image":"big","name":"Carol"}],"total":1});
    What is my fault?

  2. #2
    Sencha User
    Join Date
    Jan 2012
    Posts
    7

    Default

    Hi,
    Did you already set up a model for your data store? Otherwise you should do that first.

    I dont think you should have a script tag that loads the product.php in the index.html file. The store proxy settings will load the product.php file from the store.

    There is no need to specify a callback key unless you specifically want to name it as a name is auto generated and then picked up as $_GET['callback'] in the products.php - you can remove
    callbackKey: 'callback',
    from the proxy definition.
    Hope this helps
    Steve

  3. #3
    Sencha User
    Join Date
    Aug 2011
    Posts
    35

    Default

    Hi,

    Thanks for the fast answer! Yes i have set a model in my store, see the model property from the store.

    In my index.html i set the scripttag( <script src="http://www.domain.com/product.php?callback=getData"></script> )

    Edit:
    When i load my store for the second time Chrome display the message:

    XMLHttpRequest cannot load http://www.domain.com/product.php?_d...art=0&limit=25. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

  4. #4
    Sencha Premium User mrsunshine's Avatar
    Join Date
    Sep 2008
    Location
    Germany - Darmstadt
    Posts
    693
    Answers
    21

    Default

    you should not add the jsonp url script tag to your index.html

    this will be done automatically by the framework if the jsonp call is tiggert

  5. #5
    Sencha User
    Join Date
    Jan 2012
    Posts
    7

    Default

    I'm sure you can remove
    <script src="http://www.domain.com/product.php?callback=getData"></script>
    from index.html as its not needed. The jsonp call is handled from the store proxy after load when the javascript is executing.

  6. #6
    Sencha User
    Join Date
    Aug 2011
    Posts
    35

    Default

    Ok, thanks guys. I removed the scripttag and try load the store i get a error.

    XMLHttpRequest cannot load http://www.domain.com/product.php?_d...art=0&limit=25. Originhttp://localhost is not allowed by Access-Control-Allow-Origin.



  7. #7
    Sencha User
    Join Date
    Aug 2011
    Posts
    35

    Default

    has anyone an idea? Is this a bug in the jsonp proxy?

Posting Permissions

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