首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用服务器代码使用API

使用服务器代码使用API
EN

Stack Overflow用户
提问于 2014-05-27 13:42:15
回答 3查看 143关注 0票数 0

我已经被提供了一个ajax API来从客户端PHP门户网站读取数据。

我让下面的代码正常工作:

代码语言:javascript
复制
    <!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数组把我搞糊涂了。

谢谢

EN

回答 3

Stack Overflow用户

发布于 2014-05-27 14:00:32

在ajax调用中使用url:"PageName.aspx/FunctionName“。

代码语言:javascript
复制
 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方法。

代码语言:javascript
复制
[WebMethod(EnableSession = false)]
public static string FunctionName(string data)
{
    // your businees logic
    return "abc";
}
票数 0
EN

Stack Overflow用户

发布于 2014-05-27 14:14:59

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

票数 0
EN

Stack Overflow用户

发布于 2014-05-27 14:35:15

请使用以下代码

ASPx代码

代码语言:javascript
复制
<%@ 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#代码

代码语言:javascript
复制
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;
        }
    }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23881811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档