Richiamare WS aspnet tramite Javascript

venerdì 18 luglio 2014 - 15.38
Tag Elenco Tags  C#  |  .NET 3.0  |  .NET 3.5  |  .NET 4.0  |  Javascript  |  XHTML 2.0

vankraster Profilo | Newbie

Salve ragazzi sto provando (impazzendo) a richiamare una funzione di un WS ma non ci riesco.
Vorrei specificare che il WS è raggiungibile da fuori in quanto ho aggiungo al web.config:

<webServices>
<protocols>
<add name="HttpGet"/>
<add name="HttpPost"/>
</protocols>
</webServices>

ho provato ad aggiungere anche:
<system.net>
<settings>
<servicePointManager expect100Continue="false" />
</settings>
</system.net>

Di seguito il codice di index.html:

<script type="text/javascript" language="javascript">

function makePOSTRequest(url, parameters) {
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
http_request = new XMLHttpRequest();
if (http_request.overrideMimeType) {
// set type accordingly to anticipated content type
// http_request.overrideMimeType('text/xml');
http_request.overrideMimeType('text/html');
}
} else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
alert("not good at all..");
}
}
}
if (!http_request) {
alert('Cannot create XMLHTTP instance');
return false;
}

http_request.onreadystatechange = confirmResponse;
http_request.open('POST', url, true);
http_request.setRequestHeader("Content-Type", "application/json");

http_request.send(parameters);
}

function confirmResponse() {
if (http_request.readyState == 4) {
alert("STATUS: " + http_request.status);
if (http_request.status == 200) {
result = http_request.responseText;
document.getElementById('databox').innerHTML = result;
alert('Response:\n\n' + result);
}
else {
alert('There was a problem with the request.');
}
}
}

function getData() {
var url = 'http://localhost:54916/scripts/outputws.asmx/HelloWorld';
var request = makePOSTRequest(url, "");
}
</script>


Vorrei aggiungere anche il contenuto di outputws.asmx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Runtime.Serialization.Json;
using System.Web.Script.Services;

namespace EasyCommerce.scripts
{
/// <summary>
/// Descrizione di riepilogo per OutputWS
/// </summary>
[WebService(Namespace = "http://microsoft.com/webservices/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// Per consentire la chiamata di questo servizio Web dallo script utilizzando ASP.NET AJAX, rimuovere il commento dalla riga seguente.
[System.Web.Script.Services.ScriptService]
public class OutputWS : System.Web.Services.WebService
{

[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string HelloWorld()
{
return "CLAUDIO";
}
}
}


Il problema è che il http_request.status è sempre 0 (zero) ... Perché , dove sbaglio?




Grazie..

alx_81 Profilo | Guru

>Salve ragazzi sto provando (impazzendo) a richiamare una funzione
>di un WS ma non ci riesco.
ciao,
che ne dici di provare ad utilizzare jQuery che ti semplifica di molto la vita? Leggi questo post:
http://forums.asp.net/t/1934215.aspx?Using+jQuery+ajax+to+call+asmx+webservice+methods
Alessandro Alpi | SQL Server MVP
MCP|MCITP|MCTS|MCT

http://blogs.dotnethell.it/suxstellino
http://suxstellino.wordpress.com
http://mvp.microsoft.com/profiles/Alessandro.Alpi

vankraster Profilo | Newbie

dovevo aggiungere in WEB.CONFIG il seguente
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>

funziona anche il mio esempio per chi avra bisogno in futuro.
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2025
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5