1

Tried applying below code after opening Chrome in terminal with allow-file-access-from-files (did latter because without was hitting an origin 'null' CORS security prob) but still no luck uploading local mac folder of images (.jpg) to a website (below quoted error message).

Any help with where I'm going wrong would be greatly appreciated. I'm very new to this - the webpage is just my .txt file.

i_stack

    var folder = "imgFolder/";

$(document).ready(function(){

$.ajax({
    url : folder,
    success: function (data) {
        $(data).find("a").attr("href", function (i, val) {
         if(val.match(/\.(jpe?g|png|gif)$/)) {
                $("body").append( "<img src="+ folder + val +">" );
            }  
        });
    }
});

Cannot read property 'match' of undefined.

i_stack
  • 23
  • 1
  • 8

2 Answers2

0

The issue is that not all your <a> elements have href attributes. For those elements, val will be undefined.

You can easily refine your selector to

$(data).find('a[href]').attr(...)

This way, you're guaranteed to only be looking at <a> elements with an href attribute.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • Thanks Phil for the response. Changing that line to $(data).find('a[href]').attr("href", function (i, val) { gets rid of the match undefined error, which is nice. But alas, the images still do not appear. i_stack – i_stack Jan 29 '19 at 04:39
  • Try logging the data returned, ie `console.log(data)`. Is it HTML? Does it have any `` elements? – Phil Jan 29 '19 at 04:45
  • This is the excerpt containing the only three, apologies - I guess being new I am overlooking something very obvious:
    Oh, no! This server is sending data Google Chrome can't understand. Please report a bug, and include the raw listing.

    Index of LOCATION

    – i_stack Jan 29 '19 at 04:49
0

For anyone interested in kind of a solution, the initially referenced code worked after I downloaded MAMP and configured it to bypass the CORS prob.

I can't speak at all to the appropriateness (efficiency, security etc.) of this method but I have casually come across cautions relating to CORS bypass.

Thanks again Phil for humouring me. It seems I was barking up the wrong tree initially.

i_stack

i_stack
  • 23
  • 1
  • 8