Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何让这个jquery邮政编码地址检索器使用cURL?_Php_Jquery_Curl - Fatal编程技术网

Php 如何让这个jquery邮政编码地址检索器使用cURL?

Php 如何让这个jquery邮政编码地址检索器使用cURL?,php,jquery,curl,Php,Jquery,Curl,下面的代码可以工作,但是当我发布页面时,我得到了跨域不允许的错误。。。这里的人告诉我使用cURL,但我尝试过,但看不到适合cURL代码来实现这一点 有人能帮我解释一下吗?请记住,这是我第一次接触卷发 先谢谢你 (页面为php) 我认为您可以使用$.ajax()而不是$.getJSON()将跨域选项设置为true,如下所示: $.ajax({ type: "GET", url: url, dataType: "json", crossDomain: true,

下面的代码可以工作,但是当我发布页面时,我得到了跨域不允许的错误。。。这里的人告诉我使用cURL,但我尝试过,但看不到适合cURL代码来实现这一点

有人能帮我解释一下吗?请记住,这是我第一次接触卷发

先谢谢你

(页面为php)


我认为您可以使用
$.ajax()
而不是
$.getJSON()
跨域
选项设置为
true
,如下所示:

$.ajax({
    type: "GET",
    url: url,
    dataType: "json",
    crossDomain: true,
    success: function(json){
                       //giving the return values to the other form inputs
                       $("#cad_endereco").val(json.logradouro);
                       $("#cad_bairro").val(json.bairro);
                       $("#cad_cidade").val(json.cidade);
                       $("#cad_uf").val(json.estado);

                   },
    error: function(){
                    //if it fails, do something
               }
});

客户端需要向服务器发送ajax请求以获取邮政编码。然后,服务器通过curl向
correiosapi.apphb.com
发出HTTPS请求,并使用curl调用结果响应ajax请求

$(function(){
    $("#cad_cep").blur(function(){
        var cep = $(this).val().replace(/[^0-9]/, '');
        // make a request to your own server
        $.getJSON('/getpostal.php?cep=' + cep, function(json){
            // this json comes from your own server.
            $("#cad_endereco").val(json.logradouro);
            $("#cad_bairro").val(json.bairro);
            $("#cad_cidade").val(json.cidade);
            $("#cad_uf").val(json.estado);
         }).fail(function(){
             //if it fails, do something
         });
    });
});
在自己服务器的根目录上创建一个名为getpostal.php的文件

<?php
// make a curl call to get the postal code data
$ch = curl_init('https://correiosapi.apphb.com/cep/' . $_GET['cep']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
// set the content type and echo the curl response.
header('Content-Type: application/json');
echo $json;

客户端需要向服务器发送ajax请求以获取邮政编码。然后,您的服务器通过curl向
correiosapi.apphb.com
发出http请求,并用curl调用结果响应ajax请求。。。所以我做了一个中间页来获取数据,然后在我的表单页上使用它,这样我就可以得到“不允许跨域”?我提供了一个答案,请看一看。“数据类型”缺少一个逗号。。。但我仍然得到“不”访问控制-允许-来源“哦。我的。上帝成功了!!这是我认为永远不会发生的事情!非常感谢你!
<?php
// make a curl call to get the postal code data
$ch = curl_init('https://correiosapi.apphb.com/cep/' . $_GET['cep']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);
// set the content type and echo the curl response.
header('Content-Type: application/json');
echo $json;