api无法正常工作无法在'XMLHttpRequest'上执行'setRequestHeader'

时间:2019-01-29 17:34:36

标签: javascript jquery api

我不确定如何更改标题名称以使api工作 ,这是api https://market.mashape.com/montanaflynn/gender-guesser

这是我下面的错误

statusText:语法错误:无法在'XMLHttpRequest'上执行'setRequestHeader':'Access-Control-Allow-Origin:https://montanaflynn-gender-guesser.p.mashape.com/?name=Aaron'不是有效的HTTP Header字段名称

<!doctype html>
<html>
<head>
    <title>Love Calculator API</title>
<meta charset="utf-8"/>

    </head>
<body>

    <h1>Facts</h1>

    <input type="text" name="name" id="name">

    <br><br>

    <input type="submit" value="Get a Fact" id="submit">

    <div id="response"></div>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


    <script>

        $(document).ready(function(){

            $('#submit').click(function(){

                var name = $('#name').val();

                $.ajax({
                    method: "GET",
                    dataType: "json",
                    url: "https://montanaflynn-gender-guesser.p.mashape.com/?name=Aaron",
                // data: { fragment: false, json: true },        https://market.mashape.com/montanaflynn/gender-guesser
                    data: { name: name },
                    success: function(data){
                        console.log( data );
                        $('#response').html('Fact: ' + data.gender + data.description );
                    },
                    error: function(jqXHR,textStatus){
                        console.log( jqXHR );
                        console.log( textStatus );
                    },
                    beforeSend: function(xhr){
                        xhr.setRequestHeader( 'X-Mashape-Key', '7OByASqCq9msh5cE6uU43zz9HxOwp1kzb1ajsn26OZDiaXH06E' );
                        xhr.setRequestHeader( 'Accept', 'application/json' );
                        xhr.setRequestHeader( 'Access-Control-Allow-Origin: https://montanaflynn-gender-guesser.p.mashape.com/?name=Aaron ' );
                        xhr.setRequestHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
                        xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');                    
                    }






                });

            });

        });

    </script>


</body>
</html>

0 个答案:

没有答案