我想要做的僅僅是將Wcf服務的Json響應提供給Wpf WebControl。我已經測試了Wcf服務,並且可以在REST客戶端中看到Json響應。Awesomium Wpf WebControl從wcf服務讀取json響應
我基本上已經嘗試了兩種方法(感謝慷慨開發誰在這裏分享自己的代碼): -
下面是我ResourceInterceptor如何構建ResourceResponse。從文檔ResourceResponse只是一個原始數據塊和指定的MIME類型的包裝。這應該意味着我應該能夠將我的迴應與contentType一起傳遞,並且awesomium應該能夠識別。但我的Ajax請求所有的土地由在 「錯誤」 的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();
}
}
我的Ajax請求很簡單,如下: -
$.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);}
});
我得到的是: -
{ 「readyState的」 0 「responseText的」: 「」,狀態:0, 「狀態文本」: 「錯誤」}
- 我使用Userscripts辦法從這裏也試過: - http://answers.awesomium.com/questions/2289/can-i-use-userscripts-or-greasemonkey-scripts-in-a.html
在我的JavaScript請求我簡單地稱爲上面這樣的工具: -
uScriptHelper.xmlHttpRequest({url:urlBase+'/list', onload=function(){return(this.responseText);}});
我可以看到這個responseText正在被Userscripts設置。但是我的ajax響應仍然是一樣的 - 錯誤導致所有空參數。我在這裏做錯了什麼?