1

I am trying to create download link using jQuery post() method as following:

$.post('file.php', {name:'name', id:'id'},
function(data){
    $('#downloadFrame').src = data.url;
});
return true;

but always data.url returns null/unidentified, also creating a form and submitting it might be applicable in my code. I have searched many posts on the forum but none of them works.

thank you.

3
  • What does file.php return?
    – FixMaker
    Nov 22, 2012 at 10:05
  • it should return binary data, according to the input (pdf, xml, ...). also when I try to alert 'data', it did shows the file bytes. Nov 22, 2012 at 10:29
  • In that case, using JQuery's .post() method probably won't work. For a start, the frame won't see the Content-Type header and so can't work out how the returned data should be displayed. The method you mention about using a form to submit the request is probably a better way to go. This method is discussed in this question.
    – FixMaker
    Nov 22, 2012 at 11:16

1 Answer 1

0

You need to access the zero element of array data, so use data[0].url

Change

function(data){
    $('#downloadFrame').src = data.url;
});

To

function(data){
    $('#downloadFrame').src = data[0].url;
});
1
  • well, data[0], return the first byte of the php file output, and still url returns null. Nov 22, 2012 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.