First i tried consuming web services that serialized structs into XML but now I got it spewing JSON which means no more escaping strings for javascript. When dumping large bits of content into a page XML made sense to me since it was already HTML escaped, but if i wanted to send commands in the same message i would have to make sure it was JS safe too, Having an un-escaped string will kill the JS but a pooly formatted HTML will probably survive (nat that its then a an excuse for pooly format HTML), in fact it may make sense to return an XML type in the struct, i'll have to look into that. So here is how to make it workie:
in the .asmx
using System;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
namespace mcpV2
{///}
/// Summary description for WebService2
///
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
[ScriptService]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService2 : System.Web.Services.WebService
{public struct tests}
{public string thestr;}
public int theint;
[WebMethod]
[ScriptMethod]
public tests HelloWorld()
{tests ttt = new tests();}
ttt.theint = 9;
ttt.thestr ="hello worls";
return ttt;
This will spit out:
{"d":{"__type":"mcpV2.WebService2+tests","thestr":"hello worls","theint":9}}I called it with the JQUERY .ajax method :
$.ajax({I added some other stuff for error catching but you get the idea. I had some trouble with Web services making huge memory leaks with the XMLserializer, hopefully the .NET AJAX serializer is not a memory hog...
type: "POST",
url: "webservice2.asmx/HelloWorld",
beforeSend: function(xhr) {
xhr.setRequestHeader("Content-type",
"application/json; charset=utf-8");
},
dataType: "json",
success: function(msg) {
// Insert the returned HTML into the .
$('#Div1').text(msg.d.thestr);
}
});
