What I want to do is simply serve the Json response from a Wcf service to the Wpf WebControl. I have tested the Wcf service as working and I can see the Json response in the REST client.
I have basically tried two approaches (thanks to the generous developers who share their code here):-
- Resource Interceptor How to hide the cursor in Awesomium
Below is how my ResourceInterceptor constructing the ResourceResponse. From the docs ResourceResponse is simply a wrapper around a raw block of data and a specified mime-type. That should mean I should be able to pass in my response along with contentType and awesomium should recognize. But my ajax request all land up in "Error" with no content in the jqXHR :-
private ResourceResponse readWebResponse(HttpWebRequest webreq)
{
HttpWebRequest.DefaultMaximumErrorResponseLength = 1048576;
HttpWebResponse webresp = null;// = webreq.GetResponse() as HttpWebResponse;
var memStream = new MemoryStream();
Stream webStream;
try
{
webresp = (HttpWebResponse)webreq.GetResponse();
webStream = webresp.GetResponseStream();
byte[] readBuffer = new byte[4096];
int bytesRead;
while ((bytesRead = webStream.Read(readBuffer, 0, readBuffer.Length)) > 0)
memStream.Write(readBuffer, 0, bytesRead);
}
catch (WebException e)
{
var r = e.Response as HttpWebResponse;
webStream = r.GetResponseStream();
memStream = Read(webStream);
var wrongLength = memStream.Length;
}
memStream.Position = 0;
StreamReader sr = new StreamReader(memStream);
string webStreamContent = sr.ReadToEnd();
byte[] responseBuffer = Encoding.UTF8.GetBytes(webStreamContent);
// Initialize unmanaged memory to hold the array.
int responseSize = Marshal.SizeOf(responseBuffer[0]) * responseBuffer.Length;
IntPtr pointer = Marshal.AllocHGlobal(responseSize);
try
{
// Copy the array to unmanaged memory.
Marshal.Copy(responseBuffer, 0, pointer, responseBuffer.Length);
return ResourceResponse.Create((uint)responseBuffer.Length, pointer,webresp.ContentType);
}
finally
{
// Data is not owned by the ResourceResponse. A copy is made
// of the supplied buffer. We can safely free the unmanaged memory.
Marshal.FreeHGlobal(pointer);
webStream.Close();
}
}
My Ajax request is simple as below:-
$.ajax({
url:urlBase+'/list'
,success: function(dt){deferred.resolve(dt);alert('hurray')},
error: function(jqXHR, textStatus, errorThrown ){
alert('oyei oyei something went wrong'+JSON.stringify(jqXHR));
var err = eval('(' + xhr.responseText + ')');
alert(err.Message);}
});
What I get is:-
{"readyState":0,"responseText":"",status:0,"statusText":"error"}
- I've also tried using the Userscripts approach from here:- http://answers.awesomium.com/questions/2289/can-i-use-userscripts-or-greasemonkey-scripts-in-a.html
In my Javascript request I simply called above utility like this:-
uScriptHelper.xmlHttpRequest({url:urlBase+'/list', onload=function(){return(this.responseText);}});
I can see the responseText is being set by the Userscripts. But my ajax response is still all the same - error result with all empty parameters. What am I doing wrong here?