weixin_33736649 2015-09-16 12:03 采纳率: 0%
浏览 31

Ajax请求JSON数据

I try to request the following JSON Data:

 {"status":"success","id":8,"title":"Test","content":"dies ist test 12"}

With this Ajax Request:

$.ajax({
url: 'http://www.XXX.de/?apikey=XXX&search=test',
type: "GET",
dataType: 'jsonp',
success: function(data){
$('#content_test').append(data.content);
 },
 error: function(data){
 //
 }
});

It is not working. What I'm doing wrong?

  • 写回答

2条回答 默认 最新

  • 关注

    Here is an example on how to use jsonp

    $.ajax({
        url: 'http://www.XXX.de/?apikey=XXX&search=test',
        type: 'GET',        
        dataType: 'jsonp',
        jsonp: '$callback',
        success: function(data) {
            console.log(data);
            $('#content_test').append(data.content);
        },
        error: function(err) {
            console.log(err);
        }
    });
    

    Also open your development tool (Ctrl + Shift + J) and check if you have any errors in your console output.

    评论
  • weixin_33696822 2015-09-17 14:33
    关注

    My Solution::

    To get the data it is necessary to have a correct callback like this in the PHP file of WP:

    $callback = $_GET['callback'];
    $response = json_encode( $return );
    
    if ( ! empty ($callback)){
    echo $callback . '(' . $response . ')';
    } else {
    echo $response;
    }
    
    die;
    

    Ajax:

     $.ajax({
     url: 'http://www.XXX.de/?apikey=XXX&search=test&callback=?',
     type: "GET",
     dataType: 'json',
     success: function(data){
     $('#content_test').append(data.content);
      },
      error: function(data){
      //
      }
     });
    
    评论

报告相同问题?

悬赏问题

  • ¥100 r语言多元回归模型怎么看表达式
  • ¥15 PMM 9010/30P
  • ¥15 pom文件依赖管理,未找到依赖
  • ¥15 现在后端返回给我一堆下载地址,都是一个视频切片后的,如何在uniapp安卓环境下将这些分片的视频下周并且合并成原始视频并下载到本地。
  • ¥15 Unity导出2D项目运行时图片变成马赛克
  • ¥15 关于communitytoolkit.mvvm的生成器得到的代码看起来没有被使用的问题
  • ¥15 matlab中此类型的变量不支持使用点进行索引
  • ¥15 咨询第六届工业互联网数据创新大赛原始数据
  • ¥15 Pycharm无法自动补全,识别第三方库函数接收的参数!
  • ¥15 STM32U575 pwm和DMA输出的波形少一段