使用服务器代码使用API

时间:2014-05-27 05:42:15

标签: c# asp.net api webclient

我已经获得了一个ajax API来从客户端PHP门户读取数据。

我有这个代码工作:

    <!DOCTYPE HTML >
<html>
    <head>
        <title>Test</title> 
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript"> 
                function runTest() {
                    var dataToSend = {
                        'format': 'XYZ-2.0',
                        'ops': {
                            resource: 'client-Read',
                            testOps: [
                                    {
                                        action: "Read",     
                                        resource: "client",
                                        value: {
                                            "fSalary": 50000,
                                            "fHomeMortgage": 400000,
                                            "aInsurance": {
                                                "coverLife": 155000
                                            },
                                            "aPersonalDetails": {
                                                "strName": "John Smith"
                                            }
                                        }
                                    }
                                    ]
                        }
                    };

                    jQuery.ajax(url+'/api/v3', {
                        type: 'POST',
                        data: JSON.stringify(dataToSend),
                        contentType: "application/json; charset=utf-8",
                        dataType: "jsonp",
                        success: function (data) {
                            alert(data);
                        },
                        error: function (xhr, ajaxOptions, thrownError) {
                            alert(xhr.status);
                            alert(thrownError);
                        }
                    });
                }
        </script>
    </head>
<body>
    <form id="form1">
        <input id="button1" type="button" OnClick="runTest();" value="Test">  </input>            
    </form>
</body>
</html>

如何将其转换为C#?换句话说,我如何使用服务器端编码来使用相同的服务。 js数组令我困惑。

由于

3 个答案:

答案 0 :(得分:0)

使用网址:&#34; PageName.aspx / FunctionName&#34;, 在你的ajax电话中。

 jQuery.ajax(url+'/api/v3', {
                    type: 'POST',
                    data: JSON.stringify(dataToSend),
                    contentType: "application/json; charset=utf-8",
                    dataType: "jsonp",
                    success: function (data) {
                        alert(data);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    }
                });

在代码后面创建一个Web方法。

[WebMethod(EnableSession = false)]
public static string FunctionName(string data)
{
    // your businees logic
    return "abc";
}

答案 1 :(得分:0)

我建议您使用类似优秀的RestSharp(http://restsharp.org/)库的库,以便通过C#代码与您的服务进行交互。 RestSharp是Apache许可的,因此即使您的应用程序是封闭源代码,也应该可以使用它。

答案 2 :(得分:0)

请使用以下代码

ASPx代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PHP.aspx.cs" Inherits="Web.PHP" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
        <title>Test</title> 
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            function runTest() {
                var dataToSend = {
                    format: "XYZ-2.0",
                    ops: {
                        resource: "client-Read",
                        testOps: [
                                    {
                                        action: "Read",
                                        resource: "client",
                                        value: {
                                            fSalary: 50000,
                                            fHomeMortgage: 400000,
                                            aInsurance: {
                                                coverLife: 155000
                                            },
                                            aPersonalDetails: {
                                                strName: "John Smith"
                                            }
                                        }
                                    }
                                    ]
                    }
                };
                $.ajax({
                    url: "PHP.aspx/v3", type: "POST", dataType: "json",
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(dataToSend),
                    success: function (data) {
                        alert(data);
                    },
                    error: function (xhr, ajaxOptions, thrownError) {
                        alert(xhr.status);
                        alert(thrownError);
                    }
                });
            }
        </script>
    </head>
   <body>
    <form id="form1" runat="server">
        <input id="button1" type="button" onclick="runTest();" value="Test">  </input>            
    </form>
</body>
</html>

代码隐藏的C#代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;

namespace Web
{
    public partial class PHP : System.Web.UI.Page
    {
        public class aPersonalDetails
        {
            public string strName { get; set; }
        }
        public class aInsurance
        {
            public int coverLife { get; set; }
        }
        public class value
        {
            public int fSalary { get; set; }
            public int fHomeMortgage { get; set; }
            public aInsurance aInsurance { get; set; }
            public aPersonalDetails aPersonalDetails { get; set; }
        }
        public class testop
        {
            public string action { get; set; }
            public string resource { get; set; }
            public value value { get; set; }
            //value
        }
        public class op
        {
            public string resource { get; set; }
            public testop[] testOps { get; set; }
        }
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        public static bool v3(string format, op ops)
        {
            bool result = false;
            //Write code here 
            return result;
        }
    }

}