# Thread: xmlHttpRequest not working for email script

1. Registered User
Join Date
Apr 2017
Posts
4

## xmlHttpRequest not working for email script

I'm trying to pull a msg file from the server to display in a basic email reader script so Outlook isn't needed. I'm struggling every which way. The console log says there's no JavaScript on the page... even though the script type='text/javascript'. I just get a blank page with no errors!

Code:
var message;
console.log('Testing console');
function httpGetAsync(theUrl, callback){
var xmlHttp = new XMLHttpRequest();
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
message = xmlHttp.responseText;
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}

httpGetAsync('\Test.txt', function(resp) { \\tried 'Test.txt' also... really need to be able to input 'z:\whole\path\to\test.txt'
console.log(resp);
});

//This part is email reader that works with 3 other external scripts
function isSupportedFileAPI() {
return window.File && window.FileReader && window.FileList && window.Blob;
}

function formatEmail(data) {
return data.name ? data.name + " [" + data.email + "]" : data.email;
}

$(function () { if (isSupportedFileAPI()) {$('.src-file').change(function () {
var selectedFile = message; //removed Browse button... this.files[0];

if (!selectedFile) {
$('.msg-info, .incorrect-type').hide(); return; } if (selectedFile.name.indexOf('.msg') == -1) {$('.msg-info').hide();
$('.incorrect-type').show(); return; }$('.msg-example .msg-file-name').html(selectedFile.name);
$('.incorrect-type').hide(); // read file... var fileReader = new FileReader(); fileReader.onload = function (evt) { var buffer = evt.target.result; var msgReader = new MSGReader(buffer); var fileData = msgReader.getFileData(); if (!fileData.error) {$('.msg-example .msg-from').html(formatEmail({name: fileData.senderName, email: fileData.senderEmail}));

$('.msg-example .msg-to').html(jQuery.map(fileData.recipients, function (recipient, i) { return formatEmail(recipient); }).join('<br/>'));$('.msg-example .msg-subject').html(fileData.subject);
$('.msg-example .msg-body').html( fileData.body ? fileData.body.substring(0, Math.min(500, fileData.body.length)) + (fileData.body.length > 500 ? '...' : '') : '');$('.msg-example .msg-attachment').html(jQuery.map(fileData.attachments, function (attachment, i) {
return attachment.fileName + ' [' + attachment.contentLength + 'bytes]' +
(attachment.pidContentId ? '; ID = ' + attachment.pidContentId : '');
}).join('<br/>'));
$('.msg-info').show(); // Use msgReader.getAttachment to access attachment content ... // msgReader.getAttachment(0) or msgReader.getAttachment(fileData.attachments[0]) } else {$('.msg-info').hide();
$('.incorrect-type').show(); } }; fileReader.readAsArrayBuffer(selectedFile); }); } else {$('.msg-example').hide();
\$('.file-api-not-available').show();
}
});

2. You do not "GET" when posting a form, you "POST" the form and you have to use the command xmlhttp.send( null ) because you are saying send nothing and the form needs to send data for it to work and you don't need any JQuery to get or for fomatting the email, you cn do that on the server, just get the raw data to the server, validate it and then send it in an email. The form data needs to be passed as a parameter because yu won't be getting data to the server, you use something like xmlhttp.send( document.forms.yourFormName ) you can pass the form object itself. you also have to set the correct enctype attribute in HTML form element, again you need a web form to do this and if you don't have a web form element then this is another problem to resolve as well.

3. Registered User
Join Date
Apr 2017
Posts
4
Remember remember and thank you for your reply \\.\, however it's not an email form. There are pdf and msg files on the server that are correspondence or information about a project. I'm able to display the pdf's quite easily, but I'm having a problem with the msg files. I found a msg reader script, and since it runs on the client it seems to me there would need to be a call to the server, basically a clickable icon link... to GET the file via xmlHTTPrequest, then display it using the basic reader script so Outlook isn't needed. The basic email reader script was made use of a Browse button to find the msg file. I can't use that of course, because the msg file is on the server. I've tried a number of ways... in the example above, I had tried assigning it with var selectedFile = message; but it just displays blankness with no error.

4. Registered User
Join Date
Apr 2017
Posts
5
Hi, the first thing i see that maybe you use "Internet Explorer" and are not solving the XDomainRequest problem?

If it's not that, then you could use "Chrome" (for example) and check your site with it's "developer tools". There you'll find error messages and further options to test your site.

Hope that helps...

5. Registered User
Join Date
Apr 2017
Posts
5
did you include jQuery?

6. Registered User
Join Date
Apr 2017
Posts
5
Originally Posted by mirkov
Hi, the first thing i see that maybe you use "Internet Explorer" and are not solving the XDomainRequest problem?
Sorry, forget that part... i'm actually, probably pretty tired. :-) jQuery makes the AJAX-call happen...
but i would maybe find a full app that works out of the box.. instead of trying to fix something that doesn't... (just in case you didn't write it yourself)

Cheers

7. Registered User
Join Date
Apr 2017
Posts
4

## Nowhere Fast

I'm mostly testing in Firefox, sometimes Chrome... less often IE. This is as close to "out of the box" as I could find. I've tested the email reader on the client side and it works fine. It seems to be mostly a matter of getting an xmlHTTPRequest to pull a file from the Server instead of using a Browse button to find a file on the local machine... because I'm eliminating the Browse button entirely. I tried outputting xmlHTTPRequest to message; then assigned var selectedFile = message; but it gets me a blank page with no errors.

8. Registered User
Join Date
Apr 2017
Posts
4
I've got xmlHTTPRequest working... it was a network error issue, but I get a 404 page now when I try to display .msg file. The file is there, because I can change the extension to .txt and it will display jibberish... (maybe I'm there, and it just needs to be plugged into the reader script somewhere?) Besides that, I thought if I used overrideMimType it would help, but it displays [object blob].