氣象數據接口 調用教程

項目說明地址:http://www.36wu.com/Service/Details 

C# 後臺調用需安裝所需的包 NuGet運行    PM> Install-Package ServiceStack.Client

後臺代碼

using ServiceStack;
using System;

namespace _36wu.com_Demo
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //所需的包 PM> Install-Package ServiceStack.Client
            //獲取json格式
            var client = new JsonServiceClient("http://api.36wu.com");

            //獲取jsv格式
            //var client = new JsvServiceClient("http://api.36wu.com");

            //GET請求
            //Label1.Text = client.Get<string>("/Weather/GetWeather?district={0}".Fmt(TextBox1.Text.Trim()));
            //object obj = client.Get<反序列化爲你需要的對象>("/Weather/GetWeather?district={0}".Fmt(TextBox1.Text.Trim()));

            //POST請求
            Label1.Text = client.Post<string>("/Weather/GetWeather", new { district = TextBox1.Text.Trim() });

            //未來一週氣象
            //client.Post<string>("/Weather/GetMoreWeather", new { district = TextBox1.Text.Trim() });

            //未來一週氣象 
            //client.Post<string>("/Weather/GetMoreWeatherByIp", new { ip ="" });
        }
    }
}


html頁面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="_36wu.com_Demo.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
    <script src="Scripts/jquery-2.1.3.min.js"></script>

    <script>
        $(document).ready(function () {
            $("#JqButton1").click(function () {
                //$.getJSON("http://api.36wu.com/Weather/GetMoreWeather", { district: $("#TextBox1").val().trim() }, function (result) {
                //    $("#Label1").text(JSON.stringify(result))
                //})

                //jsonp
                $.ajax(
                    {
                        type: "post",
                        url: "http://api.36wu.com/Weather/GetMoreWeather",
                        data: { "district": $("#TextBox1").val().trim() },
                        dataType: "jsonp",
                        success: function (result) {
                            $("#Label1").text(JSON.stringify(result))
                        }
                    });


                //$.ajax(
                //    {
                //        type: "post",
                //        url: "http://api.36wu.com/Weather/GetMoreWeather",
                //        data: { "district": $("#TextBox1").val().trim() },
                //        dataType: "json",
                //        success: function (result) {
                //            $("#Label1").text(JSON.stringify(result))
                //        }
                //    });
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="Label1" runat="server" /><br />
            <asp:TextBox ID="TextBox1" runat="server" Text="101010200" />
            <asp:Button ID="Button1" runat="server" Text="C# 後臺獲取示例" OnClick="Button1_Click" />
            <input id="JqButton1" type="button" value="Jquery獲取示例" />
        </div>
    </form>
</body>
</html>

以上jQuery  請求必須需 dataType 參數 

響應速度很快 僅僅20多毫秒


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章