Example of C# WebService collapsed domain call

 

1) Get to know WebService (C#)

Take a look at current application development and you'll find an absolute trend: people are starting to prefer browser-based client-side applications. This is of course not because the client can provide a better user interface, but because it can avoid the high cost of desktop application distribution. Publishing desktop applications is expensive, half because of application installation and configuration issues, and the other half because of communication issues between client and server.

Web Service technology enables different applications running on different machines to exchange data or integrate with each other without resorting to additional, specialized third-party software or hardware. Applications implemented according to the Web Service specification can exchange data with each other regardless of the language, platform or internal protocol they use. Web Service is a self-describing, self-contained, available network module that can perform specific business functions. Web Services are also easy to deploy because they are based on some conventional industry standards and some existing technologies, such as XML, HTTP, a subset of the standard Universal Markup Language. Web Services reduce the cost of application interfaces. Web Services provide a common mechanism for the integration of business processes across an enterprise or even among multiple organizations.

1) Create a new project using WebService

Just create an empty ASP.NET project.

Then right-click on the project, add-new item, and select Web Service.

In order to make the ajax request to the method, the [System.Web.Script.Services.ScriptService] annotation must be removed, and a new

The GetVal method receives the request id and returns a random number.

Create a new static page, import js and jq into the page header

The page has an input box and an a tag. When the a tag is clicked, the ajax submission is triggered, and the data of the text box is submitted to the GetVal method of the WebService1.asmx service. After success, the returned data is added and displayed to the element whose id is data.

Things that need to be added in the Web.config file for cross-domain calls. If it is not added, the WebService file cannot be accessed in the release under IIS.

Run the program and open HtmlPage1.html.

Enter 5, click Get Data, and the returned data has been displayed.

 

 Code:

WebService1.asmx:

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

namespace WebServiceDemo
{
/// <summary>
/// Summary description of WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this web service to be called from script using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{

[WebMethod]
public string HelloWorld(int a,int b)
{
int sesult = a + b;
return sesult.ToString();
}

[WebMethod]
public string GetVal(string id)
{
return "The input is: " + id + "; auto-generated number: " + new Random().Next(10, 41);
}
}
}

 

 HtmlPage1.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script typet="text/javascript" src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>
<!--<script typet="text/javascript" src='http://libs.useso.com/js/jquery/1.9.1/jquery.min.js'></script>-->
</head>
<body>
<input type="text" id="var1"/><a id="getdata" href="javascript:void(0);">获取数据</a>
<div id="data"></div>
<script type="text/javascript">
$(function () {
$("#getdata").click(function () {
$.ajax({
type: 'POST',
url: 'WebService1.asmx/GetVal',
data: '{ id:"' + $("#var1").val() + '"}',
dataType: 'json',
contentType: "application/json",
success: function (data) {
$("#data").append(data.d);
}
});
});
});
</script>
</body>
</html>

 

Inside the Web.config <webServer> node

<!--异域调用所需-->
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Headers" value="Content-Type"/>
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS"/>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="HttpSoap"/>
<add name="HttpPost"/>
<add name="HttpGet"/>
<add name="Documentation"/>
</customHeaders>
</httpProtocol>

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324726324&siteId=291194637