特殊字符在通过API时会被加密

时间:2016-08-11 09:22:09

标签: asp.net-web-api

我正在开发全球天气API。

    <!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8" />
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>

    <script type="text/javascript">

        $(document).ready(function () {
            alert("success1");

            $('#ClickData').click(function () {
                var CountryDetails = "524901&APPID=3a93f181f74c615547db27a418662aa4";
                //$('#TextCountry').val()
                var Pdata = $('#DataArea').val();
                alert(CountryDetails);
                $.ajax({
                    //524901&APPID=e962f6f0d29167e22903aaa90e72203f
                    url: 'http://api.openweathermap.org/data/2.5/forecast/city',
                    method: 'get',
                    data: { id: CountryDetails },
                    dataType: 'json',
                    success:
                        function (Final)
                        {
                            if (Final.weather[0].message != null)
                            {
                                Pdata.html(Final.weather[0].message);
                                alert("success3");
                            }
                                //alert("success");
                            else
                            {
                                alert("success5");
                                Pdata.html(Final.weather[0].main + '<br\>' + Final.weather[0].description);
                            }
                    }
                });
            });
        });
    </script>
</head>
<body>
    Country <input type="text" id="TextCountry" />
    <input type="button" value="Click" id="ClickData" /><br /><br />

    <p id="DataArea"></p>
</body>
</html>

当我传递appkey ID时(这是访问Web API所必需的..问题是我将“ http://api.openweathermap.org/data/2.5/forecast/city?id=524901&APPID=3a93f181f74c615547db27a418662aa4 ”数据传递给APi,其中as调用像'&amp;'&amp;'='这样的特殊字符时会像这样加密“ http://api.openweathermap.org/data/2.5/forecast/city?id=524901%26APPID%3D3a93f181f74c615547db27a418662aa4

请帮助我解决这个问题。

1 个答案:

答案 0 :(得分:0)

AJAX请求只能看到一个参数id并转义具有特殊含义的字符=&。您需要单独传递查询参数。

例如:

$.ajax({
    url: 'http://api.openweathermap.org/data/2.5/forecast/city',
    method: 'get',
    data: {
        id: '524901',
        APPID: 'e962f6f0d29167e22903aaa90e72203'
    },
    dataType: 'json',
    // etc
});