检索服务器的JSON文件 [英] retrieve json file from server

查看:97
本文介绍了检索服务器的JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的服务器的JSON文件。到现在为止,我总是需要一个JSON文件,它得到了阿贾克斯,并在服务器上的PHP文件创建JSON文件。

I am trying to get a json file from my server. Until now, always I need a json file, it was got by ajax and a php file in server creates the json file.

不是我有一个JSON文件(X.json)这种结构:

Not I have a json file (X.json) with this structure:

{
"zona": [
    {
        "zona1": [
            {
                "lon": "a",
                "lat": "b"
            },
            {
                "lon": "aa",
                "lat": "bb"
            },
            {
                "lon": "aaa",
                "lat": "bbb"
            },
            {
                "lon": "aaaa",
                "lat": "bbbb"
            }
        ]
    },
    {
        "zona2": [
            {
                "lon": "c",
                "lat": "d"
            },
            {
                "lon": "cc",
                "lat": "dd"
            },
            {
                "lon": "ccc",
                "lat": "ddd"
            },
            {
                "lon": "cccc",
                "lat": "dddd"
            },
            {
                "lon": "ccccc",
                "lat": "ddddd"
            }
        ]
    }
]
}

当我试图以同样的方式来获取文件,我没有得到任何东西。我想,也许这是可能的,当我打开像一个javascript文件的网页添加文件。或者,也许有JSONP但我TRID,也是我不好回答。

And when I try the same way to get the file, I didn't get anything. I think maybe it is possible to add the file when I load the webpage like a javascript file. Or maybe with jsonp but I trid and also I got bad answer.

为JSON试试,我用:

As json try, I used:

$.ajax({
        url: 'localhost/open/listaPuntosZona.json',
        type: 'GET',
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        jsonp: "callback",
        jsonpCallback: "jsonpCallbackfunction",
        error: function () {
            alert("Error in Jsonp");
        }
    });

function jsonpCallbackfunction(responseData) {
alert(responseData);    

}

此外,我包裹JSON文件:回调(jsonfile code)

Also I wrapped json file with: callback( jsonfile code)

和也,这另外两个尝试:

And also, this other two tries:

$.ajax({ 
  url: 'localhost/open/listaPuntosZona.json', 
  type: 'get', 
  error: function(data){ }, 
  complete: function(data){ 

    data=jQuery.parseJSON(data); //do something with data

    alert(data.zona.zona1.length);
  }
});



$.getJSON('localhost/open/listaPuntosZona.json',function(jsonData){
    alert("hola");
    alert(jsonData);
});

我使用LAMPP来测试网页。

I am using lampp to test the webpage.

我需要改变些什么?我用JSONP在过去,但不知道我做错了。

Do I have to change something? I used jsonp in past but don't know what I am doing wrong now.

推荐答案

首先,请尝试使用 $。的getJSON(),而不是简单的$。阿贾克斯()。它可以解决很多问题了蝙蝠的权利。

First of all, try using $.getJSON() instead of plain $.ajax(). it will solve many problems right off the bat.

http://api.jquery.com/jQuery.getJSON/

其次,请确保您的JSON文件是完全格式化,没有任何松动的人物和奇怪的空格。

Secondly, make sure your json file is formatted perfectly, without any loose characters and strange whitespaces.

也尽量链中的错误处理程序到你的Ajax调用。可用上述的getJSON文档。

Also try to chain an error handler to your ajax call. available in the getJSON documentation above.

var jqxhr = $.getJSON("example.json").error(function() { alert("error"); });
// this is according to documentation, i cannot currently test this to work, sorry about that.

这篇关于检索服务器的JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆