Heim  >  Fragen und Antworten  >  Hauptteil

javascript – Domainübergreifende Lösung für lokale Anrufe, Taobao-API

Heute habe ich auf /q/10 die erste Antwort gesehen, dass Taobao eine API für Mobiltelefonnummern hat, also habe ich lokal eine HTML-Datei geschrieben und eine Ajax-Anfrage an diese API gesendet. Es kam jedoch zu einem domänenübergreifenden Fehler . Es wurde nicht verarbeitet. Bitte posten Sie den Code. Vielen Dank, Xiaobai

世界只因有你世界只因有你2684 Tage vor644

Antworte allen(3)Ich werde antworten

  • phpcn_u1582

    phpcn_u15822017-05-19 10:19:45

    JSONP。。

    Antwort
    0
  • 迷茫

    迷茫2017-05-19 10:19:45

    可以通关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>

    这是之前写的应用jsonp跨域的测试文件,你可以参考

    Antwort
    0
  • PHPz

    PHPz2017-05-19 10:19:45

    <!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>
    

    Antwort
    0
  • StornierenAntwort