【问题标题】:Special character are getting encrypted while passing through API特殊字符在通过 API 时被加密
【发布时间】:2016-08-11 09:22:09
【问题描述】:

我正在开发一个全球天气 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,就像在调用时一样像“&”和“=”这样的特殊字符正在像这样“http://api.openweathermap.org/data/2.5/forecast/city?id=524901%26APPID%3D3a93f181f74c615547db27a418662aa4”进行加密

请在这方面帮助我如何处理这种情况。

【问题讨论】:

    标签: asp.net-web-api


    【解决方案1】:

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

    例如:

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

    【讨论】:

    • 如果它解决了您的问题,您能接受答案吗?谢谢
    猜你喜欢
    • 2016-04-22
    • 2014-06-06
    • 1970-01-01
    • 2012-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-09-09
    • 2017-01-19
    • 1970-01-01
    相关资源
    最近更新 更多