Doubt about $.getJSON on pages using PHP Templates

Asked

Viewed 111 times

0

I know and already know how to use the method $.getJSON jQuery returning data that is returned with PHP by json_encode().

It turns out, in the project I’m developing, I use a Template (PHP) for better organization in the matter of HTML and PHP. And, for example, I am in the index of my site and I click on a link (that makes the function for the $.getJSON) that is directed to meusite.com.br/Igor. In this folder (/Igor) there is a index php. which is the file responsible for defining variables and displaying the template (HTML).

In that case, you can use the $.getJSON to return such data? If not, how to proceed in that case?

Thank you!

  • If your index.html returns HTML, the browser will only get HTML. You would have to use $.get instead of $.getJSON (the get "guess" the type, but if you want more security can pass dataType : "html" in the parameters). But if you want JSON, the server has to return JSON. You could for example pass a pro parameter to your index.php saying whatever is returned, if feasible. But from the server’s point of view, if there is nothing indicating what the browser whether, it will behave in the default way - which is to return an HTML.

  • But if I use $.get("/Igor")... it will "detect" the existing index.php in the folder and load it?

  • Both the $.get as to the $.getJSON just make an Ajax request, nothing else. It’s no different than you go to the address bar and type in the URL. Mapping between a URL and a server resource (a static document, a template, an image...) occurs only on the server, the browser has nothing to do with it.

  • Yes, I know that, but I don’t think you understand my question. Just as the browser detects index.php when accessing a folder on a website $.get detects whether the request is a folder? (Ex.: /contact)

  • The browser detects nothing if you ask /contato he will ask /contato pro server. It is the server that will decide which /contato maps to a file or a template. I mean, it doesn’t matter if you type in the URL bar, if you use $.ajax (or $.get or $.post), if you make a curl from the command line... In all cases, you are asking /contato pro server, and it is he who decides the right way to respond to that request.

  • Wait! You’re saying that /Igor redirects for /index.php? (type, returning an HTTP code 30X?)

  • No, the /Igor has an index.php from which it generates a template of an html file (practically a require of an html file)

  • So it’s still worth what I said before (by the way, even if it was a redirect, would still be valid - because Ajax works normally even with a redirect). Can use $.get no problems that the server will know what to do (i.e. receiving a request for /Igor, run PHP and return the result).

  • Hmmm so ok. Thank you so much for your help! In the morning I will do some tests and then I will return here to talk about the results.

  • Yeah, it really worked! The problem is that I had some other doubts, but then they are for another question. Hehe. Thank you very much for the tips!

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.