我有一个Web服务,返回下载URL。
[WebMethod]
[ScriptMethod]
public string GetExportURLAll(string types)
{
string[] typesArr = types.Split(',');
var ctx = HttpContext.Current;
var rootDirectory = ctx.Server.MapPath(@"~/");
rootDirectory = rootDirectory + "Admin\\";
DataExporter exp = null;
int[] ids = {0};
string relDirectoryName = DataExporter.RelDirectoryName();
List<string> combinedFileNames = new List<string>();
foreach (string type in typesArr)
{
exp.createFolder(rootDirectory, relDirectoryName);
exp.getObject(ids, true, type, combinedFileNames);
}
return exp.archive(relDirectoryName, combinedFileNames);
}
getObject运行几秒钟,因此javaScript不会等待返回的值并继续运行:
function exportSelectedData() {
var url;
var check=["a","b","c"];
...
if (check.length > 0) {
check = check.join(",");
WebService.GetExportURLAll(check, function (url) {
document.location = url;
}, function (err) {
console.error(err);
});
}
}
行document.location = url;在返回url之前开始执行并且它给出了错误。
1. _errorObject: undefined
2. _exceptionType: undefined
3. _message: "The server method 'GetExportURLAll' failed."
4. _stackTrace: undefined
5. _statusCode: 0
6. _timedOut: false
但是如果我设置一个断点并等待C#完成,那么一切都很完美。
我尝试用回调来做,但它给了我同样的错误
function exportSelectedData() {
getExportURL(check, function () {
saveFileToDesktopByUrl("DataExport.zip", "application/zip", url)
});
}
function getExportURL(check, callback) {
RiekerPortal.MapWebService.GetExportURLAll(check, function (url) {
callback();
});
我总是最终得到
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, checkhttp://xhr.spec.whatwg.org/.
Setting 'XMLHttpRequest.withCredentials' for synchronous requests is deprecated.