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

时间:2014-12-10 16:34:11

标签: php jquery curl

下面的代码有效,但是当我发布页面时,我得到了跨域不允许的错误......这里的人告诉我使用cURL,但我试过并且不能看到符合cURL代码来做到这一点。 ...

有人可以为我启发这件事吗?请记住,这是我第一次接触cURL

提前谢谢

(页面是php)

$(function(){
           /**
            * act when the postal code field gets blur
            */
           $("#cad_cep").blur(function(){
               /**
                * Retrieve the value on postal code field
                * use replace to erase non-numeric inputs
                * with a regular expression
                */
               var cep = $(this).val().replace(/[^0-9]/, ''); 

              //Creates the variable with the json url appending the postal code
             var url = 'https://correiosapi.apphb.com/cep/'+cep;

              /**
              * using $.getJSON to get the request return;
              */

              $.getJSON(url, 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);

                       }).fail(function(){
                        //if it fails, do something
                   });

           });
       });

2 个答案:

答案 0 :(得分:2)

客户端需要向服务器发送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;

我不能保证这会起作用,但它应该让你开始。

答案 1 :(得分:0)

我认为您可以使用$.ajax()代替$.getJSON()设置crossDomain选项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
               }
});
相关问题