简体   繁体   English

ajax发布外部php文件始终返回false

[英]ajax post external php file return always false

my ajax code is : 我的ajax代码是:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
    $.ajax({
            type: "GET",
            contentType: "application/json; charset=utf-8",
            url: "http://www.mp3-y.com/yt_output.json?vid=d9o8FCl4HOk",
            dataType: "json",
            success: function(msg) {
            alert("Success");
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert("error");
            }

        });
</script>

PHP Code is : PHP代码是:

header('Content-Type: application/json');
$arr=array("Download" => "http://www.exemple.com/file");
$json = json_encode($arr);
print_r($json);

when i call the external php file i get always error! 当我调用外部php文件时,我总是出错!

Browsers will not allow your javascript to issue a XHR call to another domain (mp3-y.com) unless they allow it by specifying a specific header. 浏览器不允许您的JavaScript发出对另一个域(mp3-y.com)的XHR调用,除非他们通过指定特定的标头允许它。 Research "same origin policy". 研究“同源政策”。

If you can change your php code, you can add this header to allow anyone to call this API: 如果可以更改PHP代码,则可以添加此标头,以允许任何人调用此API:

<?php
header('Access-Control-Allow-Origin: *');  
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM