E
E
Evgeny Zhurov2018-02-07 12:40:53
PHP
Evgeny Zhurov, 2018-02-07 12:40:53

How to get json content generated by php in WordPress CMS?

Good afternoon, the story is this - a multidimensional array is formed in php, serialized and placed in a file:

$serialArr = serialize($arr);
file_put_contents('cards.json', $serialArr);


In the future, I try to get its contents:

$.getJSON('cards.json', function(data) {
    var output = '<ul>';
    $.each(data, function(key, val) {
        output += '<li>' + val + '</li>';
    });
    output += '</ul>';
    $("body").html(output);
});


But I don't get anything. At the same time, apparently, the script sees the file itself - because if you change the file name or the path to it in js, then an error pops up in the console that the file was not found. Those. path is correct. The 100% file is not empty. No console.log() inside $.getJSON is printed either. What is done wrong?

Site on wordpress - I don’t know, maybe there are some features related to this. For example, maybe this json needs to be placed in some specific directory, or something else?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Justique, 2018-02-07
@Zhuroff

$serialArr = serialize($arr);
Isn't it?
$serialArr = json_encode($arr);

O
Oleg Polyakov, 2018-02-07
@challenger1401

It must be placed in a directory accessible via the web. Those. if json-opens in the browser, then in the script too.
$.getJSON('cards.json' - instead of cards.json there should be a path from the address bar of the browser, because this is how it looks for this file from the current page

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question