javascript - Cross-domain solution for local calling Taobao api
世界只因有你
世界只因有你 2017-05-19 10:17:45
0
3
639

Today I saw the first answer on /q/10... that Taobao has an api for mobile phone numbers, so I wrote an html file locally and sent an ajax request to this api. However, it prompted a cross-domain error. No I have dealt with this kind of problem, how to solve it? Please post the code~ Thank you very much, Xiaobai~

世界只因有你
世界只因有你

reply all(3)
phpcn_u1582

JSONP。。

迷茫

Cross-domain can be done via JSONP

<script>
$(document).ready(function(){ 
    $("#search").click(function(){ 
        $.ajax({ 
            type: "GET",     
            url: "http://127.0.0.1:8000/ajaxdemo/serverjsonp.php?number=" + $("#keyword").val(),
            dataType: "jsonp",
            jsonp: "callback",
            success: function(data) {
                if (data.success) {
                    $("#searchResult").html(data.msg);
                } else {
                    $("#searchResult").html("出现错误:" + data.msg);
                }  
            },
            error: function(jqXHR){     
               alert("发生错误:" + jqXHR.status);  
            },     
        });
    });
    
    $("#save").click(function(){ 
        $.ajax({ 
            type: "POST",     
            url: "http://127.0.0.1:8000/ajaxdemo/serverjsonp.php",
            data: {
                name: $("#staffName").val(), 
                number: $("#staffNumber").val(), 
                sex: $("#staffSex").val(), 
                job: $("#staffJob").val()
            },
            dataType: "json",
            success: function(data){
                if (data.success) { 
                    $("#createResult").html(data.msg);
                } else {
                    $("#createResult").html("出现错误:" + data.msg);
                }  
            },
            error: function(jqXHR){     
               alert("发生错误:" + jqXHR.status);  
            },     
        });
    });
});
</script>

This is the test file for applying jsonp cross-domain written before, you can refer to it

PHPzhong
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>test</title>
</head>

<body>
    <input type="text" id="tel">
    <input type="button" id="btn" value="submit">
    <script src="https://lib.sinaapp.com/js/jquery/3.1.0/jquery-3.1.0.min.js"></script>
    <script>
    $('#btn').click(function() {
        var tel = $('#tel').val().trim();
        if (!tel) {
            return;
        }
        $.ajax({
                url: 'https://tcc.taobao.com/cc/json/mobile_tel_segment.htm',
                dataType: 'jsonp',
                data: {
                    tel: tel
                },
            })
            .done(function(rs) {
                console.log(rs);
            })
            .fail(function() {
                console.log("error");
            })
            .always(function() {
                console.log("complete");
            });

    });
    </script>
</body>

</html>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!