【发布时间】:2014-09-10 14:09:37
【问题描述】:
我正在尝试通过调用我自己的服务器从 script.js 执行 iplookup,但返回的只是“未定义”。为什么?
iplookup.php
<?php
header('content-type: application/json; charset=utf-8');
$data = json_encode($_SERVER['REMOTE_ADDR']);
//next line needs to be commented out
//echo $_GET['callback'] . '(' . $data . ');';
?>
script.js
//get ipaddress
$.ajax({
url: "http://www.example.com/iplookup.php",
data: null,
type: 'GET',
crossDomain: true,
dataType: 'jsonp'
}).done(function (json) {
self.ip = json;
});
//on the next line it returns `undefined`
console.log('ipaddress: ' + self.ip);
【问题讨论】:
-
因为您没有在 PHP 中回显任何内容。
-
为什么是
jsonp?它究竟在哪里返回undefined? -
当您尝试回显时,$data 的响应是什么?
标签: php json undefined ip-address lookup