ho una pagina di esempio che chiama un servizio web per prelevare il cognome del cliente in base al suo ID qui pagina:
http://applicazioni.vsc300.it/Mediweb2015/Prova.aspx
questo è il servizio:
codice:
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.<System.Web.Script.Services.ScriptService()> _
<WebService(Namespace:="http://tempuri.org/")> _
<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Public Class Service
Inherits System.Web.Services.WebService
'<WebInvoke(RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest,Method="GET")>
<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
PublicFunctionCercaPaziente(IDPazienteAsInteger)AsString
Dim serialiser AsJavaScriptSerializer=NewJavaScriptSerializer()
DimPazAsNew CLS_Paziente
Paz.GetCercaAnagrafica(IDPaziente)
Dim serializer AsJavaScriptSerializer=NewJavaScriptSerializer()
Dim serializedItems AsString= serializer.Serialize(Paz)
Return serializedItems
EndFunction
EndClass
come potete notare mi da errore di:
Only Web services with a [ScriptService] attribute on the class definition can be called from script. eppure ho tolto il commento nella prima riga per abilitare la chiamata da script
devo fare altro? il dubbio mi viene in quanto ho ricreato il servizio da un'altro programma scritto in C# he aveva questo header nella funzione:
codice:
<WebInvoke(RequestFormat=WebMessageFormat.Json,ResponseFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.WrappedRequest,Method="GET")>
per ora ho lasciato webmessageFormat Json, devo fare altro?
grazie
per completezza ecco il codice jquery che richiama il servizio
codice:
<script type="text/javascript"> $(document).ready(function () {
var params = { 'IDPaziente': 6586 }
$.ajax({
"dataType": 'json',
"contentType": "application/json; charset=utf-8",
"type": "GET",
"url": "../Service.asmx/CercaPaziente",
"data": params,
"success": function (msg) {
var json = jQuery.parseJSON(msg.d);
//valorizza texbox
$("#TXT_CognomePaziente").val(json.Denominazione);
},
error: function (xhr, textStatus, error) {
alert(error);
}
});
});