Main Page | Report Page

 

  .NET DotNet Forum Index » ASP.NET Forum » Consume webservice from havascript getting json...

Author Message
Lorenzo...
Posted: Fri Dec 11, 2009 8:57 am
 
Dear all,
I've been experiencing frustrating problems and can't fix them.

I'm using Asp:net 3.5, FireFox 3.5.5, IE 8, Coolite Toolkit Community,
Version : 0.8.0, Last Updated : 2009-05-26 (as per the ReadMe.txt)


I need to call a web service from javascript code passing some
parameters, so I wrote the folowing (the web method is a fake, but
return a list of the same object I'd like to return)

***Web Service:***

namespace WebService1
{
[WebService(Namespace = "http://tempuri.org/")]
[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.
[ScriptService]
public class HighlightStreet : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json,
UseHttpGet = false,
XmlSerializeString = false)]
public List<Rendition> GetRendition(string pippo)
{
int numParams = Context.Request.QueryString.Count;
List<Rendition> renditions = new List<Rendition>();

renditions.Add(new Rendition("123456", pippo + "ren123456"));
renditions.Add(new Rendition("789123", pippo + "ren789123"));

return renditions;
}
}

public class Rendition
{
public string mslink { set; get; }
public string renditionId { set; get; }

public Rendition(string _mslink, string _renditionId)
{
mslink = _mslink;
renditionId = _renditionId;
}

public Rendition() { }
}
}


***JavaScript PART:***

Ricerca.gerRenditionId = function(mslinks)
{

Ext.Ajax.request({
url: 'http://localhost/site/WS/HighlightStreet.asmx/GetRendition',
success: function(p1, p2)
{
alert("Successo");
},
failure: function(p1, p2)
{
alert("fallimento");
},
headers: {
'Content-type': 'application/json; charset=utf-8'
},
params: { 'pippo': 'pippolandia123' },
method: 'post'
});
}


I've 2 different behaviourS in FF and IE 8:
***FF:***
The function enter the "success" branch BUT the response is in XML
format instead of JSON, even if 1) I've marked the webmethod with
"ResponseFormat = ResponseFormat.Json" and 2) add "'Content-type':
'application/json; charset=utf-8'" to headers.
FireBug reports the following:

RequestHeaders:
Host: localhost
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.9.1.5)
Gecko/20091102 Firefox/3.5.5 (.NET CLR 2.0.50727)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
X-Requested-With: XMLHttpRequest
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost/GasGis/default.aspx
Content-Length: 20
Cookie: ASP.NET_SessionId=mxjg4e45cmzsmh3eguqmnh45
Pragma: no-cache
Cache-Control: no-cache


and then get the following ResponseHeaders:

Server: Microsoft-IIS/5.1
Date: Wed, 09 Dec 2009 13:29:54 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 438


The ResponseBody is an XML (but I do want a JSON):

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfRendition xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Rendition>
<mslink>123456</mslink>
<renditionId>pippolandia123ren123456</renditionId>
</Rendition>
<Rendition>
<mslink>789123</mslink>
<renditionId>pippolandia123ren789123</renditionId>
</Rendition>
</ArrayOfRendition>

The ResponseBody show the the "pippo" parameter has been correctly
forwarded and used.

***IE 8 behaviour:***
I get an esxception in the responseText:

"{"Message":"Primitiva JSON non valida: pippo.","StackTrace":"
in
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()
in
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32
depth)
in
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String
input, Int32 depthLimit, JavaScriptSerializer serializer)
in
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer
serializer, String input, Type type, Int32 depthLimit) in
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](String
input)
in
System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext
context, JavaScriptSerializer serializer)
in
System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData
methodData, HttpContext context)
in
System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext
context, WebServiceMethodData
methodData)","ExceptionType":"System.ArgumentException"}"


No JSON nor XML response but that Exception.

I don't know where to look anymore; some threads say something about the
httphandlers in web.config, but mine should be fine:

<httpHandlers>
<remove path="*.asmx" verb="*" />
<add path="*.asmx" verb="*"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"
validate="false" />
<add path="*_AppService.axd" verb="*"
type="System.Web.Script.Services.ScriptHandlerFactory,
System.Web.Extensions, Version=3.5.0.0, Culture=neutral,
PublicKeyToken=31BF3856AD364E35"
validate="false" />
<add path="ScriptResource.axd" verb="GET,HEAD"
type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions,
Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
validate="false" />
<add path="*/coolite.axd" verb="*"
type="Coolite.Ext.Web.ResourceManager"
validate="false" />
</httpHandlers>


Any help would be very very appreciated and.
If any other info is needed to get a solution, just let me know.

Many thanks,

Lorenzo
 
 
Page 1 of 1    
All times are GMT - 5 Hours
The time now is Wed Aug 31, 2016 1:04 am