Javascript uses XMLHTTP to access the WebService of Asp.net 2.0

Source: Internet
Author: User

After reading a lot of data on the Internet, it is basically the same. It is not very convenient to enter parameters on the WebService and then use soap to transmit values. Every time you write a lot of data

Or in the common XMLHTTP mode, you only need to submit a simple parameter value pair, like: http: // localhost/AS. aspx? Xxx = BB format

 

The post method is as follows:

Note: The scui in the following code is a pre-written component package, which includes an asynchronous XMLHTTP component.

 

This component provides a setformparm (formid) method input parameter

Setparam (PARAM) input simple parameters

Sendrequestbypost (func, URL) Method Request server WebService

Createxmlhttp () initializes the XMLHTTP object for Creation

 

For example, the WebService page SC _test.asmx is old and sayhello

 

Using system;
Using system. Web;
Using system. collections;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. Data;
Using system. Data. sqlclient;
Using system. text;

 

/// <Summary>
/// SC _webservice Summary
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
Public class SC _webservice: system. Web. Services. WebService {

Public SC _webservice (){

// If you use the designed component, uncomment the following line
// Initializecomponent ();
}

[Webmethod]
Public String sayhello ()
{
Httprequest request = system. Web. httpcontext. Current. request;
String name = request. Params ["name"];
Return "hello" + name;
}

 

}

 

To use httpcontext in WebService, you must add the following code to the Web. config file:

<WebService>

<Protocols>

<Add name = "httpget"/>

<Add name = "httppost"/>

</Protocols>

</WebService>

 

Client page

 

<HTML>

<SCRIPT type = "text/JavaScript" src = "../util/JS/commonengine. js"> </SCRIPT>

<! -- The above is a package -->

<SCRIPT type = "text/JavaScript">

// Use XMLHTTP in scui

Function Test (){

VaR fui = new scui ();

Fui. createxmlhttp ();

Fui. setparam ("name =" + document. getelementbyid ("name"). value );

 

// In POS Mode

Fui. sendrequestbypost (function (){

If (Fui. XMLHTTP. readystate = 4 & Fui. XMLHTTP. Status = 200 ){

// XML data returned by WebService

VaR reqxml = Fui. XMLHTTP. responsexml;

// Obtain the value

VaR text = (reqxml. childnodes) [1]. Text );

Alert (text );

Fui = NULL;

}

}, "SC _test.asmx/sayhello ");

}

 

// The preceding alert (text) value can get the sayhello data.

 

// The following is part of the code for XMLHTTP in scui.

Function scui () {This. XMLHTTP = false; this. paramstring = "";}

// Create XMLHTTP, which is relatively old and has a lot of online content, which is omitted here

 

// Send the request in POS Mode

Scui. Prototype. sendrequestbypost = function (C, URL ){

This. XMLHTTP. Open ("Post", URL, true );

This. XMLHTTP. setRequestHeader ("Content-Type", "application/X-WWW-form-urlencoded; charset = UTF-8 ");

This. XMLHTTP. onreadystatechange = C;

This. XMLHTTP. Send (this. paramstring );

}

// Set parameters

Scui. Prototype. setparam = function (s ){

This. XMLHTTP. paramstring = s; // If garbled characters (such as the Java background) appear in the background, use encodeuri (s) to encode the code twice and then decode it in the background, asp.net generally does not have too many problems, so you do not need to use
}

 

 

</SCRIPT>

I am still studying Asp.net. I used to mainly use Java. If any of the above is incorrect, please point out
 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.