Use JavaScript to access the Web service that transmits data through JSON

Source: Internet
Author: User

JSON is a popular data protocol that can be easily parsed by JavaScript. However, the default data serialization method of web services developed by Asp.net is XML, in order to study how to enable Web Services to interact with clients in JSON data format, I made the following attempts.

My development environment is: Visual Studio 2008 +. net framework3.5 + iis7, which should be pointed out here. net framework2.0 can also be developed, but you need to add an independent system. web. extensionsProgramSet Reference, and make the corresponding configuration in the web. config file. For details, see the following introduction. On the client I directly access the Web service through JavaScript and use the jQuery-1.2.5 script library.

Web service file

For asmx files, you need to add relevant attributes to the Web service class so that they can serialize and deserialize data in JSON format. Here we use system. web. script. the scriptservice in the services namespace modifies the Web service class. Note that, in terms of security, Web Service methods only support post access by default. If you want to access through get, you need to add scriptmethod (usehttpget = true) in the corresponding method) attribute.CodeSection:

< WebService ( Namespace: Namespace : = " Http://tempuri.org/ " ) > _
< Webservicebinding (conformsto: = Wsiprofiles. basicprofile1_1) > _
< Scriptservice () > _
< Global. Microsoft. VisualBasic. compilerservices. designergenerated () > _
Public   Class Service Class Service
Inherits System. Web. Services. WebService


/**/ ''' <Summary>
'''Gets the auto completed terms.
''' </Summary>
< Webmethod () > _
< Scriptmethod (usehttpget: = True ) > _
Public   Function getterms () Function Getterms ( Byval Query As   String ) As List ( Of   String )
Dim Listterms As List ( Of   String ) =   New List ( Of   String )

For Index As   Integer   =   1   To   10
Listterms. Add (Query + Index. tostring ())
Next

Return Listterms
End Function

End Class

Web. config configuration file

If you use. net framework3.5, you do not need to modify the web. config File (configured automatically when the project is created. add System in net framework2.0. web. extensions Assembly reference method. After adding a reference, the configuration file must be modified as follows to ensure that the service can be correctly handled. The code at this end is used to unmap the asmx file and process the asmx file by using the scripthandlerfactory class:

< Httphandlers >
< Remove Verb = "*" Path = "*. Asmx" />
< Add Verb = "*" Path = "*. Asmx" Validate = "False" Type = "System. Web. Script. Services. scripthandlerfactory, system. Web. Extensions, version = 3.5.0.0, culture = neutral, publickeytoken = 31bf3856ad364e35" />

Page files

The page file to reference the jQuery-1.2.5 version of the script library, jquery is a small but powerful client javascrui script library, can help us complete the script development work.

< Script Type = " Text/JavaScript " >
Function Fncallsvc () {
$. Ajax ( {
URL: ' Service. asmx/getterms ' ,
Type: ' Post ' ,
Contenttype: ' APP/JSON; charset = UTF-8 ' ,
Success: Function (MSG) {
$ ( ' # Mylist ' Pai.html ( '' );
For ( VaR I = 0 ; I < MSG. D. length; I ++ ) {< BR >$ ( ' # mylist ' ). append ( '

  • ' + MSG. d [I] + '
  • ' );
    }
    } ,
    Datatype: ' JSON ' ,
    Data: ' {Query :" ' + $ ( ' # Editquery ' ). Val () + ' "} ' ,
    Processdata: False
    } );
    }
    < / SCRIPT>

    The above code sends a POST request to our web service and passes in the query parameter to the getterms method. In the above Code, note the following:

      • Contenttype must be set to application/JSON; charset = UTF-8; otherwise, normal JSON data cannot be returned.
      • Data is the parameter value passed to the web service. JSON objects cannot be directly transmitted here, But JSON serialized strings (as shown in the Code) should be passed because.. net.

    The above is an attempt to access web services using JavaScript.

    Reference:

    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.