【发布时间】:2016-05-10 14:20:43
【问题描述】:
我是 CefSharps Chromium 浏览器的新手,很难弄清楚如何获取 jquery ajax 请求的结果。
我的第一次尝试是将我的 AJAX 请求传递给EvaluateScriptAsync。实际上脚本有效。它完全符合我的要求,但我没有得到任何结果/状态代码,因为我的 Cef-Task 不会等到 AJAX 完成其工作。
这里是一个例子(只是一个示例代码):
var tasks = pdBrowser.EvaluateScriptAsync(@"
(function(){
$.ajax({
type: ""POST"",
dataType: ""json"",
cache: false,
url: ""_resources/php/ajaxRequests.php"",
async: false,
data: {
action: ""insertCrossPlatform"",
type: """",
values: JSON.stringify(""foo bar"")
},
success: function(response) {
if (typeof response === 'string' && response.substring(0, 5) == ""ERROR"")
{
return response;
}
else
{
//pageReload();
return ""OK"";
}
},
error: function(xhr, textStatus, errorThrown) {
return errorThrown + ""\n"" + xhr.responseText;
},
complete: function() {
return ""COMPLETE"";
}
});
})();", null);
tasks.ContinueWith(t =>
{
if (!t.IsFaulted)
{
var response = t.Result;
if (response.Success)
{
if (response.Result != null)
{
MessageBox.Show(response.Result.ToString());
}
}
else
{
MessageBox.Show(response.Message, "Ein Fehler ist aufgetreten", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
}, TaskScheduler.Default);
后来我读到有一个 SchemeHandler,但我没有正确理解如何实现它。谁能帮帮我?
提前致谢。
【问题讨论】:
-
如果您在匿名闭包中返回了一个值,那么它将作为
EvaluateScriptAsync的结果返回。您可以将参数作为 json 返回,然后在C#中执行WebRequest,完成后执行更多JS以更新您的UI。我不特别推荐这种方法,请参阅下面的替代方法。
标签: javascript c# jquery ajax cefsharp