1

I have a webservice that returns URL for downloading.

    [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 runs few seconds, so javaScript doesn't wait for the returned valu and keeps going:

      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);
            });               
        }
    }

The line document.location = url; starts executing before url is returned and it gives an error.

      1.    _errorObject: undefined
      2.    _exceptionType: undefined
      3.    _message: "The server method 'GetExportURLAll' failed."
      4.    _stackTrace: undefined
      5.    _statusCode: 0
      6.    _timedOut: false

But if I put a breakpoint and wait for C# to finish, then everything is perfect.

I tried to do it with callback, but it gave me the same error

           function exportSelectedData() {
              getExportURL(check, function () {
                saveFileToDesktopByUrl("DataExport.zip", "application/zip", url)
            });
            }
        function getExportURL(check, callback) {
        RiekerPortal.MapWebService.GetExportURLAll(check, function (url) {
            callback();
        });

I always end up getting

                   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.
sozolo
  • 11
  • 1
  • Have you seen this: http://stackoverflow.com/questions/24639335/javascript-console-log-causes-error-synchronous-xmlhttprequest-on-the-main-thr . I am not positive but you might find a clue to the source of the problem in that post. – David Tansey Apr 30 '15 at 21:37

0 Answers0