jquery calls WebService and WebService output JSON

Source: Internet
Author: User
Tags eval json

(Environment: vs2008+jquery1.2.3)

It is found that the. NET comes with Ajax.NET using WebService output is JSON, where the key is when requested: Content-type:application/json;utf-8

So we just need to add a content-type:application/json;utf-8 to the post.

$.ajax({
   type: "POST",
   contentType:"application/json;utf-8",
   url: "/Server/PicLib.asmx/HelloWorld",
   success: function(msg){
   var json = eval('(' + msg + ')');
   alert(json.d);}
});


var json = eval('(' + msg + ')');

(This is to convert the returned character value to a JSON object so that you can manipulate the JSON correctly, and you can use a try to catch the error, because if the returned string is not standard JSON, it will go wrong.) )

The above JS to run successfully need to do a set of. NET to "WebService output JSON"

Modify WebService, import a property

<System.Web.Script.Services.ScriptService()>

<System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/") > _
<System.Web.Services.WebServiceBinding (ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class WebService1
    Inherits System.Web.Services.WebService

    <WebMethod()> _
    Public Function HelloWorld(ByVal str As String, ByVal name As String) As String
        Return str & name
    End Function

End Class

Add in Webconfig:

  <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"/>

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.