Retrieving JSON array in Javascript from PHP

后端 未结 3 499
我寻月下人不归
我寻月下人不归 2021-01-28 13:07

I am attempting to return a json encoded array to JS from PHP and i\'ve done so many times before but now i\'m getting a weird error. I am successfully getting the data and it\'

相关标签:
3条回答
  • 2021-01-28 13:42

    Maybe you need to send the correct HTTP header with your response. See here: https://stackoverflow.com/questions/267546/correct-http-header-for-json-file

    0 讨论(0)
  • 2021-01-28 13:58

    So I worked the code back and think this solution might work for you.

    $.ajax({
      async: "false",
      type: 'POST',
      data: {
        action: 'var1',
        albumName: 'var2'
      },
      dataType: 'json',
      url: '/controller/function',
      success: function(data) {
        $.each(data, function(index, element) {
          console.log(index);
          console.log(element.fullURL);
          console.log(element);
        });
      }
    });
    

    I can't test the ajax event however I have tested out the json you provided with the each loop and it seams to work. LINK TO FIDDLE

    var data = [{
        "caption": "",
            "fullURL": "https://lh6.googleusercontent.com/--ZKG_L-SA9c/UgqECNqP4II/AAAAAAAAA2I/i5nCa3CvKqM/s912/2010raptor_firstdrive002_opt.jpg",
            "location": "",
            "program_instance_id": "a0Ji0000001pPO6EAM",
            "tags": "",
            "thumbURL": "https://lh6.googleusercontent.com/--ZKG_L-SA9c/UgqECNqP4II/AAAAAAAAA2I/i5nCa3CvKqM/s128-c/2010raptor_firstdrive002_opt.jpg"
    }, {
        "caption": "",
            "fullURL": "https://lh3.googleusercontent.com/-kyUg7_Rul90/UgqEDIu4DhI/AAAAAAAAA2Q/WF0BAEI7smo/s912/220px-Microchip_PIC24HJ32GP202.jpg",
            "location": "",
            "program_instance_id": "a0Ji0000001pPO6EAM",
            "tags": "",
            "thumbURL": "https://lh3.googleusercontent.com/-kyUg7_Rul90/UgqEDIu4DhI/AAAAAAAAA2Q/WF0BAEI7smo/s128-c/220px-Microchip_PIC24HJ32GP202.jpg"
    }];
    
    $.each(data, function (index, element) {
        console.log(index);
        console.log(element.fullURL);
    });
    

    also good news is that your json is 100% valid so what is being passed back seams correct. Hope this helps

    0 讨论(0)
  • 2021-01-28 13:59

    The variable parsed at your $.each function is not defined. you should use data instead of parsed as data is the variable at your success callback function.

    $.each(data, function(i, index) {
        alert(index.fullURL);
        });
    
    0 讨论(0)
提交回复
热议问题