PDA

View Full Version : Cannot set content type json in proxy



jgirao
24 May 2013, 9:07 AM
Hi everyone, I am from Peru and I am new with Sencha and Ext js, I have a problem with content type, I can not set it in proxy, when I use this code I have a response of my web service:


Ext.define("OportunitiesApp.store.Oportunidades", {
extend: "Ext.data.Store",
config: {
model: "OportunitiesApp.model.Oportunidad",
proxy: {
type: 'ajax',
actionMethods: {
create: 'POST',
destroy: 'DELETE',
read: 'POST',
update: 'POST'
},
url: 'webService/wsOportunidades.asmx/ListarOportunidades',
reader: {
type: 'json',
root: 'd'
}
},
sorters: [{ property: 'estado', direction: 'ASC' }],
grouper: {
sortProperty: "estado",
direction: "ASC",
groupFn: function (record) {
//debugger;
if (record && record.data.estado) {
return record.data.estado;
} else {
return '';
}
}
}
}
});



this is my web service in .Net framework 4.5:



[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json,
UseHttpGet = false, XmlSerializeString = false)]
public List<BEOportunidad> ListarOportunidades()
{
BEOportunidad objOportunidad = new BEOportunidad();
objOportunidad.Activo = Constantes.ACTIVO;
objOportunidad.UsuarioCreacion = "ASD";


BLOportunidad bl = new BLOportunidad();
List<BEOportunidad> lstOportunidades = bl.ListarOportunidad(connstring, objOportunidad);


return lstOportunidades;
}


but this response is in xml, I want to set content type json and I add this lines in proxy:



...
proxy: {
type: 'ajax',
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
...


but I have an error in consonle in Chrome:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) http://localhost:10337/sntCRM/webService/wsOportunidades.asmx/ListarOportunidades?_dc=1369415127720






The problem is when I add content-type, without it I have a response of my web service, can someone help me please?

slemmon
28 May 2013, 11:05 AM
You're wanting to send back JSON form your .NET server, but today it's sending back XML, is that correct?

jgirao
28 May 2013, 2:34 PM
Finally, I resolved this problem setting content type in my web service, like this:


[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json,
UseHttpGet = false, XmlSerializeString = false)]
public void ListarOportunidad()
{
BEOportunidad objOportunidad = new BEOportunidad();
objOportunidad.Activo = Constantes.ACTIVO;


BLOportunidad bl = new BLOportunidad();
List<BEOportunidad> lstOportunidades = bl.ListarOportunidad(connstring, objOportunidad);


JavaScriptSerializer js = new JavaScriptSerializer();
string strJSON = js.Serialize(lstOportunidades);


Context.Response.Clear();
Context.Response.ContentType = "application/json";
Context.Response.Flush();
Context.Response.Write(strJSON);
}