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