douchi0471 2014-03-06 23:22
浏览 69

PHP,Javascript,phonegap跨域与JSONP GET错误

I'm trying to build a phonegap app in which I pass coordinates from a server side PHP script to a client side javascript using AJAX GET. I am having no problem with AJAX POST but GET is giving me some issues. Whenever I try GET with dataType: "jsonp", I get a javascript console warning:

Resource interpreted as Script but transferred with MIME type text/html......

This is weird considering I have full control over the server side script so I can convert the data to JSON. Is there a chance that the server does not support JSONP (documentation says it supports JSON though, its a free host!). I've tried adding various headers to the php script but that doesn't seem to work. Also, I'm using the chrome ripple emulator just in case this is relevant.

I've had a look at a variety of similar problems but I don't seem to be able to get to the bottom of it.

Anyhow, here's my code and any help would be much appreciated.

The PHP snippet (I'm pulling some data from a mysql table all this is working fine though):

$info = array(
  'lat'    => $row[LAT],
  'lng' => $row[LNG],
  'id' => $row[ID]
);
echo json_encode($info);

The relevant javascript code:

$.ajax({    
    type: "GET",
url: "http://whereto.bugs3.com/name.php",
    dataType: 'jsonp',
    jsonp: 'callback',
    jsonpCallback: 'jsonpCallback',
    success: function(data) {   
        alert(data.lng);
        }
    });

Thanks in advance

  • 写回答

1条回答 默认 最新

  • douyu5679 2014-03-06 23:24
    关注

    You will need to wrap your output with a "jsonpCallback" function to become jsonp (json with padding)

    echo 'jsonpCallback(' . json_encode($info) . ');';
    

    See also: http://en.wikipedia.org/wiki/JSONP#How_it_works

    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求码,CNN+LSTM实现单通道脑电信号EEG的睡眠分期评估
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路