chanchu
chanchu

Reputation: 27

kendo mobile JSON data calling

I have written a code for taking JSON data from a PHP and putting to listview, it works great in localhost. When I put the PHP file in a web server and called in Javascript it showing error and not getting data.

This method I have used:

 var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                     
                       //url: "userchk.php",    //this works in localhost  
                       url: "http://example.com/web/userchk.php",  this is not working in localhost 
                       
                        dataType: "json", // JSONP (JSON with padding) is required for cross-domain AJAX
                        data: { //additional parameters sent to the remote service
                            q: "javascript"
                        }
                    }
                },

The first url data is getting in localhost and works great, second url is not working(but data show if we run the url in a browser).

It show an error like this:

XMLHttpRequest cannot load http://example.com/web/userchk.php?q=javascript. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

waiting for a good response

Upvotes: 0

Views: 1373

Answers (2)

Debug_mode
Debug_mode

Reputation: 80

This may be due to cross domain issue. You need to use datatype:jsonp .. A sample code reading data from ODATA Version 2.0 feed is as following ,

 studentsData = new kendo.data.DataSource(
            {
                type: "odata",
                transport: {
                    read: {

                        url: "http://server/Service.svc/Students",
                        dataType: "jsonp",

                        data: {
                            Accept: "application/json"
                        }
                    }
                },
                serverfiltering: false,
                serverPaging: true,
                batch: false,
                pageSize: 10

            });

You can read the complete "Kendo UI ListView Control and OData in Windows Phone Application" post.

Thanks @debug_mode

Upvotes: 1

Mark
Mark

Reputation: 2135

This is related to the same origin policy that most web browsers implement. It seems like the php file you are trying to access is in another server other than your current one (localhost).

If you are tyring to access local:

I suggest you change the url to /web/userchk.php

If you are are trying to access another site

I suggest you change the dataType to "jsonp"

Try looking at the kendoUI datasource example with twitter. They try to access twitter (a different origin) using jsonp.

Upvotes: 1

Related Questions