jQuery getJSON url - php

I am setting up a twitter feed into my site and have it working perfectly when you access it with a root page ie www.domain.com/index.php
However when you have a url that is www.domain.com/category/page it doesn't not work, however if you use www.domain.com/index.php?cat=category&page=page it works fine.
So in the javascript I have the following
jQuery(function() {
jQuery.getJSON('cache/twitter-json.txt', function(data){
jQuery.each(data, function(index, item){
jQuery('#tweet').append('code for displaying tweet');
});
});
So I have tried to change the url in the getJSON function to the full path www.domain.com/cache/twitter-json.txt but that doesn't work.

Try:
jQuery.getJSON('/cache/twitter-json.txt', function(data){
Relative URLs are interpreted relative to the directory of the page containing the reference. So when you make the above call from www.domain.com/category/page, it tries to go to www.domain.com/category/cache/twitter-json.txt.
If you want to use the full URL, you need to put // before the hostname, e.g.
jQuery.getJSON('//www.domain.com/cache/twitter-json.txt', function(data){
Otherwise, it thinks www.domain.com/ is just another level of directory.

change
jQuery.getJSON('cache/twitter-json.txt', function(data){
to
jQuery.getJSON('/cache/twitter-json.txt', function(data){

Maybe it helps to set the base in your html
<base href="http://myserver/dir1/">

Related

How to call Ajax in wordpress in custom.php page?

I have a little weird problem
I want to call wordpress ajax url in a custom page.php in public_html
The weird thing is that I have two type of these pages which have two different path :
First one is : the_permalink()/page1
Second one is : the_permalink()/page1/page2
The problem is that the ajax url call is working fine in page1 path : the_permalink()/page1
But same functions with same way of calling is not working in page2 path : the_permalink()/page1/page2
Is there any suggestions ?
Note : i have tested the the SCRIPT code which call the ajax with alert() function and found that that the code stops when it becomes to ajax part is this code :
<script>
$(document).ready(function(){
$('.checknow').click(function(e){
e.preventDefault();
var data = "test";
//alert(ajaxurl);
$.ajax({
type:"POST",
url:ajaxurl,
data: {
action:'action_function_php',
data:data,
},
success:function(data){
$('.security-check-result').html(data);
}
});
});
return false;
});
</script>
Note : i am using wildcard *, so the permalink return is subdomain if this would help
I have also tried to type the url directly but didn't work also
Here is an example page ( will remove it soon ) - just for make things
clear -
https://gameloop.bramj.store/windows
in this page if u tried to search something in the search bar you will find the ajax return works fine
but if you move to
https://gameloop.bramj.store/windows/download
you will notice that every single ajax code in the page is not working.
Thank you so much the problem is solved
i had to put the current main url ( domain/ subdomain ) without any slugs
so the best solution for this case is use this url :
var ajaxurl = "https://<?php echo $_SERVER['HTTP_HOST'];?>/wp-admin/admin-ajax.php";
You need to use wp_localize_script in order to have access to ajaxurl global.
If you don't want to do that then you can replace ajaxurl with <?php echo admin_url('admin-ajax.php');?> if you javascript is inside a PHP file.

$.getJSON don't work in the second link page of my local site

I have a problem with the $.getJSON function. The get works fine on my first page - index.html. However, when I go to my second page - second.html and use the function $.getJSON I get an error.
The code (second.html) does not work:
$(document).ready(function(){
$('button').click(search);
});
function search(){
var name = $('#inputName').val();
$.getJSON("api/research_name.php?nome="+nome, function(data){
console.log(data);
});
}
This code (index.html) works fine:
$(document).ready(function(){
$('button').click(search);
});
function search(){
var name = $('#inputName').val();
$.getJSON("api/research_name.php?nome="+nome, function(data){
console.log(data);
});
}
(this would probably be better as a comment, but)
check your code. you assign name to the value of an input field, but in your .getJSON call use nome
Also, you should probably use urls that are fully qualified. For example, if your hierarchy is
/index.html
/folder/something/second.html
then your api call will be relative to the directory in which it was called, which might not be the root.
Consider changing it to "/api/research_name.php?nome="+nome

Calling a function in php with link in html

I'm trying to call a function with a link in html. I found the following example:
click to run function!
if(isset($_POST['runfunction'])){
}
This works perfectly fine, the problem is that when I click the link, "?runfunction" keeps standing in my url bar. So when I submit a form on my page it goes totally wrong (it's way to long to upload here). I do some SQL queries and I'm getting weird values in my SQL database. When I type in just my normal url it works fine. So I'm pretty sure that's the problem. I found another example with ajax :
$("a").click(function(){
jQuery.ajax({
url: "path/to/controller",
type: "POST",
dataType: 'json',
data: {'mentod':'ExportExcel'},
success: successCallback,
error:failureCallback
});
});
I don't fully understand this example (because I never use AJAX) but my php script is included in the html page "include("")". So I can't type in url because it has to be the same page. Can someone give a little bit of info about this, or give an example of how I can fix this? Thanks in advance!
You can add a callback method then remove it from the url by javascript
function successCallback () {
url = window.location.href;
window.location = url.replace("runfunction", "");
}

AJAX, SEO and PHP folder trouble

I'm creating an AJAX script that reads a specific file, which name is identified with a GET var. The beginning of the script in index.php is:
var expFile = '<?php echo $_GET['text_name']; ?>';
$(document).ready(function () {
$.ajax({
url: expFile+'.xml',
type: 'get',
dataType: 'xml',
success: function (data, textStatus) {
// Parses the content, which is escaped into a "text" tag in the xml, and puts it
//into an html div with a "content" class"
var content = decodeURIComponent($(data).find('text').text());
$('.content').html(content);
}
});
});
And everything went ok. The file is read and the thing is shown correctly.
The XML file, that is in the same folder of the index.php file, is read directly from AJAX.
Now i'm using a mod_rewrite in order to make the URL SEO-Friendly.
When i type the dirty URL (http://www.mysite.com/index.php?text_name=name-of-the-file-to-read) it's OK.
But when i type the rewritten url (which is http://www.mysite.com/lyrics/name-of-the-file-to-read) the content is not shown.
I know that AJAX is client-side, while mod_rewrite is server-side, but I don't know how to reach a parent folder (that really doesn't exist) from the "url" parameter of the $.ajax or an absolute link like url: 'http://...' (but it goes against Same Origin Policy).
Help me please!!!
Francesco's answer, initially put into the question:
SOLVED.
THE PROBLEM was with the script tag. I had to put a ../ to the jquery href
<script type="text/javascript" src="../libraries/jquery.min.js"></script>
Then I added ../ to the URL and everything went fine.
The URL is having .xml appended by the function itself - on this line:
url: expFile+'.xml',
Is that still there when you are trying with the SEO friendly URL? If so, I'd remove that appended .xml and try again.

Using jQuery to load external PHP page

I'm using this code to load pages onclick without reloading the current page:
$(function() {
$("#tabs a").click(function() {
var page = this.hash.substr(1);
$.get(page+".php", function(html) {
$('#content').html(html)
});
return false;
});
});
The code is working and everything's fine except that it looks for the pages to display within the same path of the current page, for example if the current page is www.mysite.com/index
It looks for www.mysite.com/index/page.php
and this page doesn't exist because I put my PHP pages in a specific folder
how to change the code to make it look for the external pages in the specific folder that I want?
.get, forms, anchors, etc. will all use relative paths if you give them relative paths. If you want them to use an absolute path, provide an absolute path. Absolute paths begin with a slash /.
For example:
$.get("/" + page + ".php" ...
This of course requires page to be the full path.
Try this: $('#content').html(html); You forgot parentheses around the jQuery selector.
$("#tabs a").click(function() {
var page = this.hash.substr(1);
$.get("php/" + page + ".php", function(html) {
$('#content').html(html);
});
return false;
});

Categories