Webservice 要output JSON格式的資料
在web service中
method 要加上
[ScriptMethod(ResponseFormat:=ResponseFormat.Json)] _
class
要先設定[ScriptService()] 這樣Jquery.ajax才能使用
[web service]
[HTML]
在web service中
method 要加上
[ScriptMethod(ResponseFormat:=ResponseFormat.Json)] _
class
要先設定[ScriptService()] 這樣Jquery.ajax才能使用
[web service]
Imports System.Net
Imports System.IO
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.Web.Script.Services
Imports System.ComponentModel
Imports net.gallerys.common.util
Imports PLaiN.dll
Imports log4net
Imports Newtonsoft.Json
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")>_
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
<ScriptService()> _
Public Class WebService
Inherits System.Web.Services.WebService
<WebMethod()> _
Public Function HelloWorld() As String
Return "Hello World"
End Function
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Function ListProduct2() As String
Dim plainDB As New PLaiNDB_CTX.PLaiNDBDataContext(ConfigurationManager.ConnectionStrings("plan_ecConnectionString").ConnectionString)
Dim v As List(Of outputPorduct) = (From p In plainDB.Products Order By p.AutoNO _
Select New outputPorduct _
With {.autono = p.AutoNO, .price = p.Price, .productId = p.ProductID} _
).Take(50).ToList()
Dim result As String = jsonParser(v)
Return result
End Function
Function jsonParser(ByVal o As Object) As String
Dim json As New JsonSerializer()
json.NullValueHandling = NullValueHandling.Ignore
Dim sb As New StringBuilder
Dim sw As New IO.StringWriter(sb)
Dim jw As New JsonTextWriter(sw)
json.Serialize(jw, o)
Return sb.ToString()
End Function
End Class
[HTML]
<script type="text/javascript">
$(document).ready(function() {
$.ajax({
type: "POST",
url: "/WService/Webservice.asmx/ListProduct2",
contentType: "application/json",
dataType: "json",
success: function(response) {
var o=jQuery.parseJSON(response.d);
alert(o[1].key);
},
error: function(msg) {
alert("ERR" +msg.toString);
}
});
});</script>
留言