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
(theget
"guess" the type, but if you want more security can passdataType : "html"
in the parameters). But if you want JSON, the server has to return JSON. You could for example pass a pro parameter to yourindex.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.– mgibsonbr
But if I use $.get("/Igor")... it will "detect" the existing index.php in the folder and load it?
– Igor
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.– mgibsonbr
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)
– Igor
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 acurl
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.– mgibsonbr
Wait! You’re saying that
/Igor
redirects for/index.php
? (type, returning an HTTP code30X
?)– mgibsonbr
No, the /Igor has an index.php from which it generates a template of an html file (practically a require of an html file)
– Igor
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).– mgibsonbr
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.
– Igor
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!
– Igor