0

我已经编写了一个从 PHP 获取 JSON 数据并将其放入 listview 的代码,它在 localhost 中运行良好。当我将 PHP 文件放入 Web 服务器并在 Javascript 中调用时,它显示错误并且没有获取数据。

这个方法我用过:

 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"
                        }
                    }
                },

第一个 url 数据进入 localhost 并且运行良好,第二个 url 不工作(但数据显示我们是否在浏览器中运行 url)。

它显示如下错误:

XMLHttpRequest 无法加载http://example.com/web/userchk.php?q=javascript。Access-Control-Allow-Origin 不允许来源 http://localhost。

等待好的回应

4

2 回答 2

1

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.

于 2012-04-16T06:10:33.007 回答
1

这可能是由于跨域问题。您需要使用 datatype:jsonp .. 从 ODATA 版本 2.0 提要读取数据的示例代码如下,

 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

            });

您可以阅读完整的“Windows Phone 应用程序中的 Kendo UI ListView 控件和 OData”帖子

谢谢@debug_mode

于 2012-06-18T07:50:06.643 回答