0

The real thing I am trying to do is understand why my dynamic linked object does not say hello when clicked (see bottom of code). But it may be because I have a preceding error--when I run this, the first test alert (in the loop) tells me the key is "error". In the console it says the parse has a syntax error on '<'...but there isn't any such char in the JSON. It is not even real ajax, just a demo:

$(document).ready(function () {
//main ajax call to populate the menu  
$.ajax({
    dataType: 'json',
    url: 'https://jsfiddle.net/afk4cpz4/',
    success: function (data) {
        $.each(data, function(key) {
            alert("The key is: "+ key);
            var myname = data[key].name;
            $("#epresults").append("<div class='inner"+[key]+" namer'>"+myname+"</a></div>");
            $(".inner" + [key] + "").wrapAll("<div id='item" + [key] + "' class='tile ditem" + [key] + "' />"); //build the tile
            $("div#item" + [key]).wrap("<a class= 'ditem" + [key] + "' href='#' />"); //wrap the tile in a link
        });
    },
    error: function (xhr, desc, err) {
        console.log(xhr);
        console.log("Details: " + desc + "\nError:" + err);
    }
});
$("#epresults").show();

$(" a ").click(function () {
    alert("Hello");
});

});

https://jsfiddle.net/jhxqkg1q/21/

What's wrong?

ColdSharper
  • 74
  • 10
  • Where's your `url` parameter in AJAX call? The `data` parameter is what you send to the server. – Artur Filipiak Aug 11 '15 at 14:04
  • In the real code it points to the json file, but since I hard coded here I ripped it out. Shouldn't it work w/o the url param? – ColdSharper Aug 11 '15 at 14:11
  • I see your point @artur-filpiak...I was thinking of the return, of course. I was just trying to simplify the example without an external file. – ColdSharper Aug 11 '15 at 14:16
  • It also looks like you're attaching your click handler to existing links before your success handler runs and attaches its links. – Clayton Leis Aug 11 '15 at 15:52

1 Answers1

0

I added MIME type application/json to my server, per the answer here:

jQuery won't parse my JSON from AJAX query

Not 100% sure this was the solution, but I stopped getting the parse error after I did this.

Community
  • 1
  • 1
ColdSharper
  • 74
  • 10