Ajax call to download file returned from RESTful service

前端 未结 3 900
后悔当初
后悔当初 2020-12-04 01:29

I am fairly new to AJAX. I am sending a request to server using AJAX. The service returns a text file. But no download box appears when data is returned. The rest service t

相关标签:
3条回答
  • 2020-12-04 01:59

    Advice is simple: you cannot download files via AJAX - it's a security policy. I mean you can download the data, but you can't save it to disk from JavaScript side.

    If you want to download a file on click, then you can just add href to you a tag. Or open a new window with file's URL.

    0 讨论(0)
  • 2020-12-04 01:59

    You can't do that directly from AJAX, but you can get around it by having an iframe that initiates the download. See Ajax File download Issue for a discussion.

    0 讨论(0)
  • 2020-12-04 02:06

    A) you don't have a callback to receive data back
    b) Add error callback to you code so you can see if there are receiving errors after the call:

        $.ajax({
        url: '/spaconsole/rest/examples/getcode',
        type: 'POST'
        success: function (data) {
            console.log('ok');
        },
        error: function (xhr) {
          console.log(xhr);
        }
        });
    

    Edit: This is if you want to display the text in page. If you want to download the file, this is not the way, you cannot use ajax

    0 讨论(0)
提交回复
热议问题