weixin_33743880 2012-10-21 15:41 采纳率: 0%
浏览 65

带有JSON的jQuery ajax函数

Hi i am using this code to get contents from a json file but i am not able to show results

my code is

$(document).ready(function(){

$.ajax({
    url: 'http://50.116.19.49/rest/user.json',
    dataType: 'jsonp',
    jsonp: 'jsoncallback',
    timeout: 50000,
    success: function(data, status){
    alert(data);    
    },
    error: function(){
        output.text('There was an error loading the data.');
    }
});
});

In google chrome i can see that the request status is 200 and data is loaded. here is the link from which i am copying code The Link.... Any help!!!!

Thanks

  • 写回答

3条回答 默认 最新

  • 普通网友 2012-10-21 17:24
    关注

    Your code doesn't work because the http://50.116.19.49/rest/user.json resource return a JSON response instead of JSONP (difference).

    So you can't make cross-domain calls via ajax BUT you can create a some proxy script ON YOUR SERVER which will make it for you. Example:

    cross-domain-call.php

    <?php echo file_get_contents('http://50.116.19.49/rest/user.json');?>
    

    And change your script to

    $(document).ready(function(){
    
    $.ajax({
        url: 'http://127.0.0.1/cross-domain-call.php',
        dataType: 'json',
        timeout: 50000,
        success: function(data, status){
            console.log(data);    
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log(jqXHR);    
            console.log(textStatus);    
            console.log(errorThrown);    
        }
    });
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用