【问题标题】:Different Result in Ajax Response when Request JSON from PHP从 PHP 请求 JSON 时 Ajax 响应中的不同结果
【发布时间】:2014-04-02 14:02:28
【问题描述】:

我有工作代码

jsfiddle

var url = 'http://ip.jsontest.com/';        
$.ajax({
    url: url,
    dataType: "jsonp",
    async: false,
    success: function (result) {
        alert('success');
    },
    error: function (request,error) {
        alert(error);
    }
});         

该代码产生“成功”。但是当我用这个 PHP 代码将 json url (http://ip.jsontest.com/) 更改为 'http://example.com' 时

<?php
header('Content-Type: application/json; charset=utf-8');
$iii = array('ip' => '114.79.28.24' );
echo json_encode($iii);
?>

我遇到了一个错误
怎么了?

【问题讨论】:

标签: php jquery json jsonp


【解决方案1】:

数据类型:“json”而不是“jsonp”

var url = 'http://ip.jsontest.com/';        
    $.ajax({
    url: url,
    dataType: "json",
    async: false,
    success: function (result) {
        alert('success');
    } ,
    error: function (request,error) {
        alert(error);
    }
 }); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-13
    • 2018-11-15
    • 2021-02-07
    相关资源
    最近更新 更多