Tôi đang sử dụng .Net framework 2.0/jQuery để thực hiện cuộc gọi Ajax tới dịch vụ web 2.0. Không có vấn đề gì tôi đặt contentType vào trong cuộc gọi ajax, dịch vụ luôn trả về XML. Tôi muốn nó trả lại Json!Cách trả về JSON từ dịch vụ web asmx 2.0
Đây là cuộc gọi:
$(document).ready(function() {
$.ajax({
type: "POST",
url: "DonationsService.asmx/GetDate",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
// Hide the fake progress indicator graphic.
$('#RSSContent').removeClass('loading');
// Insert the returned HTML into the <div>.
$('#RSSContent').html(msg.d);
}
});
});
Dưới đây là những gì tiêu đề yêu cầu trông giống như trong Fiddler:
POST /DonationsService.asmx/GetDate HTTP/1.1
x-requested-with: XMLHttpRequest
Accept-Language: en-us
Referer: http://localhost:1238/text.htm
Accept: application/json, text/javascript, */*
Content-Type: application/json; charset=utf-8
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; eMusic DLM/4; .NET CLR 2.0.50727)
Host: localhost:1238
Content-Length: 2
Connection: Keep-Alive
Pragma: no-cache
Tôi đã cố gắng thiết lập contentType để 'text/json' và nhận được cùng một các kết quả.
Dưới đây là phương pháp dịch vụ web:
<WebMethod()> _
Public Function GetDate() As String
'just playing around with Newtonsoft.Json
Dim sb As New StringBuilder
Dim sw As New IO.StringWriter(sb)
Dim strOut As String = String.Empty
Using jw As New JsonTextWriter(sw)
With jw
.WriteStartObject()
.WritePropertyName("DateTime")
.WriteValue(DateTime.Now.ToString)
.WriteEndObject()
End With
strOut = sw.ToString
End Using
Return strOut
End Function
và đây là những gì nó sẽ trả về:
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://DMS.Webservices.org/">{"DateTime":"11/13/2008 6:04:22 PM"}</string>
Có ai biết làm thế nào để buộc các dịch vụ web để trở Json khi tôi yêu cầu Json?
Xin vui lòng không cho tôi biết để nâng cấp lên .Net Framework 3.5 hoặc bất cứ điều gì như thế (tôi không phải là ngu ngốc). Tôi cần một giải pháp 2.0.
Có thể bạn có thể sử dụng giải pháp của mình; http://stackoverflow.com/questions/26140330/call-javascript-function-from-asmx – Erdogan
Bạn có thể thử giải pháp của mình; http://stackoverflow.com/questions/26140330/call-javascript-function-from-asmx – Erdogan