0

I have been provided with an ajax API to read data from client PHP portal.

I have this code working:

    <!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>

How can I convert it to C#? in other words, how can I consume the same service using server side coding. the js array is confusing me.

Thanks

MoX
  • 93
  • 1
  • 1
  • 7
  • Please see http://msdn.microsoft.com/en-us/library/hh674188.aspx, http://stackoverflow.com/questions/6620165/how-to-parse-json-in-c & http://json.codeplex.com/ – Jon P May 27 '14 at 05:50

3 Answers3

0

Use url: "PageName.aspx/FunctionName", in your ajax call.

 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);
                    }
                });

Create a web method in code behind.

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

I recommend that you use a library like the excellent RestSharp (http://restsharp.org/) library for interacting with your service from your C# code. RestSharp is Apache Licensed so it should be possible for you to use it even if your application is closed source.

wasatz
  • 4,158
  • 2
  • 23
  • 30
0

Please use below code

ASPx Code

<%@ 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# Code for Code behind

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;
        }
    }

}
Deepak Joshi
  • 1,036
  • 7
  • 17