I'm completely baffled with this. I've tried seemingly everything, and nothing has worked so far!
Basically, I have a PHP script which does some stuff and will feed out a number. Now, this PHP file is being hosted on my server and I need to extract that number from the PHP file through a HTML script which I can put anywhere.
This script works fine when both the PHP file, and the HTML file (which contains this script) are in the same directory:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.get("index.php",function(data){
document.write(data);
});
});
</script>
However, as the PHP script will be uploaded on a server, I need to be able to get the number which is displayed on the PHP script from wherever I put this code. So basically, I need to be able to use this code:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$.get("http://www.myserver.com/scripts/index.php",function(data){
document.write(data);
});
});
</script>
But I am really unsure as to why it is not working when it isn't in the same directory. I'm not looking to overcomplicate this, and just need a small snippet of code that I can use to collect the number which is fed from that PHP script. Please note that I cannot put these files in the same directory as I'm going to be using this code to feed the number onto a site-editor made website.
Thanks!
Your requests are getting blocked because of the Same-origin policy, your only choice is to use the same domain or enable cors.
So as long as your page AND php file are both placed under www.myserver.com, it will work.
JSONP Examples can be found here and here
$.ajax({
url: "http://www.myserver.com/php/file.php",
jsonp: "callback",
dataType: "jsonp",
success: function( response ) {
console.log( response ); // server response
}
});
Related
I'm learning the basics of PHP and am trying to create the most basic PHP backend possible, with a JS/jQuery frontend. My current problem is that a GET request does not execute the .php file, like I was expecting, but instead returns a whole page.
I've tried many jQuery and PHP scripts based on tutorials and stack overflow posts, and the current configuration is based on this post here.
I have read some other StackOverflow posts about this issue here, here, and here but couldn't get those solutions to work, or didn't understand the answer.
Here's my setup: I have an index.html page that will render the results of my jQuery AJAX request & a data.json.php page, where I would like a basic response to be sent from. These files are in the same directory.
--- jquery.js ---
$(function () {
$.ajax({
url: "data.json.php",
success: function(result){
alert(result);
}
});
});
---data.json.php---
<?php
header('Content-Type: application/json');
$output = "hello"
echo json_encode($output);
?>
I am using the live server addon to render the html and I have started the php server through the command php -S localhost:4000.
I do get an alert, but it's returning the contents of the .php file rather than executing it. I think that I may be misunderstanding how to start the PHP server correctly, the proper way of running a PHP script, or how the flow of information goes.
Any help, tips, or resources would be greatly appreciated. Thanks!
./test
./test/test.php
./test/test.html
#cd test
#php -S localhost:4000
test.php content:
<?php
header('Content-Type: application/json');
$output = "hello";
echo json_encode($output);
test.html content:
<html>
<head>
<script src="https://code.jquery.com/jquery-3.6.2.min.js"></script>
</head>
<body>
<script>
$(function () {
$.ajax({
url: "http://localhost:4000/test.php",
success: function(result){
alert(result);
}
});
});
</script>
</body>
</html>
how to and test video
I'm trying to use an ajax $.get() request to display calculated data within one of my WordPress pages. I haven't decided if I'm going to host the php calculation file on a different server of mine, or the one that WordPress is hosted on (don't think this would make a difference anyway since its a data request). I'm looking to add the following code to either the header.php file, or even better, within the page created in wp-admin:
<script>
$(document).ready(function(){
$.get("http://my-other-website.com/parse-list.php",function(data){
alert(data);
});
});
</script>
I know this is a basic function, but I'm just really stumped on how it doesn't work within WordPress, but works when on my other server that doesn't host WordPress. I tested jQuery to make sure it was being loaded properly, with a simple alert("hey");, and the alert works just fine. Has anyone else had this problem? Any help would be greatly appreciated!
jQuery in WordPress runs in noConflict mode which means the global $ shortcut for jQuery isn't available. Replace your code with the following:
<script>
jQuery(document).ready(function($){
$.get("http://my-other-website.com/parse-list.php",function(data){
alert(data);
});
});
</script>
So, I would like to know how to parse an xml document with JavaScript.
I've got it working in php (view below)
<?php
$xmlGmailFeed = file_get_contents("https://gmail_username:gmail_password#mail.google.com/mail/feed/atom/");
$unreadMessages = $xmlGmailFeed->fullcount;
echo $unreadMessages;
?>
But the only thing with this, is that it's running using the server's IP address and doesnt let users login to their gmail. It pops up saying that there was a suspicious login attempt on their account. So what Id like to know is how I would be able to do the same thing, but run it from the hosts computer. I was thinking javascript to do it? But please let me know if theres a better way!
Edit:
Heres the code that im using..
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js" type="text/javascript"></script>
<script>
$.ajax({
type: "GET",
url: "https://mail.google.com/mail/feed/atom.",
dataType: "xml",
crossDomain: true,
success: parseXml
});
function parseXml(xml)
{
var results = $(xml).find("fullcount").text();
alert(results);
}
</script>
Here's a screen shot of my problem:
http://cbog.co.nr/SC.php?img=Admin/AE0BCBAA4F532BC69A932E5DDD8F14F2
and you can see for yourself, live at:
http://cbogausch.com/test.php
I believe you can just use a DOMParser. jQuery also offers .parseXML() if you're willing to use it.
for the life of me I cannot get my PHP file to be called using the ajax function. Here is my relevant HTML:
$(document).ready( function(){
$("#generate").click(function(){
alert("Been clicked");
$.ajax({
url: "filterScriptForMe.php",
success: function(data){
alert("response");
}
});
});
});
Here is my PHP file, which I can run by calling separately, and will print to the error log/echo/create the necessary image no problem when run on it's own:
error_log("script has been called");
$hello = imagecreatefrompng("./images/stock.png");
$hello = imagecreatefrompng("./images/stock.png");
imagealphablending($hello,false);
imagesavealpha($hello,true);
$x=imagecolorallocatealpha($hello,0,0,0,127);
$color = imagecolorat($hello,350,500);
for($y=0;$y<512;$y++)
for($z=0;$z<597;$z++)
{
if($color!=imagecolorat($hello,$y,$z))
{
imagesetpixel($hello,$y,$z,$x);
}
}
imagepng($hello,"done.png");
echo "done";
I am beginning to wonder if there is a problem on my server at this point. When run it will print the first alert saying "been clicked" but it does not seem to be running the php script as I get no output to my log files when clicking the button from the html file, as opposed to visiting the php directly in the broswer. Both the html and php are in the same directory, and I have double checked the names to make sure they are correct.
I know other jQuery functions are working on the page as well, as I've been successful in getting some jQueryUI elements to display and respond to manipulation using jQuery alone. Added to that the first alert seems to be firing, so I'm fairly sure the library/code is in there. I've tried going through with firebug, but it's a little beyond me when it starts getting deep into the library and I lose track of what's being done and why.
Any help that you can provide would be much appreciated, at least to get the PHP file called from the html page. Thanks!
i have a simple setup, with php pulling html content from a database. if i go to my template page, it loads the data and returns it to the browser via a simple php echo ... not rocket science.
now, i've written an html file using jquery and an ajax call. i load the html file in my browser and the javascript / ajax query works.
when i load the html into the database and print it out via the php / echo, the content is the exact same when i view the source, but the ajax query doesn't execute.
<html>
<head>
<title>random query - api - get</title>
<script src="http://code.jquery.com/jquery-1.5.min.js"></script>
</head>
<body>
<pre>
executing the GET method
<script language="javascript" type="text/javascript">
$.ajax({
type: "GET",
url: "http://some-rand.om.api.com",
data: "011b38b8-011f-4b03-bb21-4c5bb26600b3",
success: function(msg){
alert( msg );
}
});
</script>
</pre>
</body>
</html>
Any comments / suggestions would be great. What I find bizarre, is i have no problem copying the source of http://jquery.com/ and pasting it into the db and doing the php / echo. this works fine. maybe an onLoad() would help...hm.
The issue is outlined in the following url:
XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin
When I load the javascript console in chrome I get this:
XMLHttpRequest cannot load http://some-rand.om.api.com/user?011b38b8-011f-4b03-bb21-4c5bb26600b3. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
To resolve the problem, on the api vhost, in the code that serves the content accessed by the ajax query, i added this (it is also php):
header('Access-Control-Allow-Origin: http://some-rand.om.client.com');
Now when I access it, it's all good and loads as expected. So, infact, it wasn't related to it being stored in the database, php or javascript. It was related to cross site scripting.
Or, to be super lazy (probably not a great idea...):
header('Access-Control-Allow-Origin: ' . $_SERVER["HTTP_ORIGIN"]);
If in your description of the problem you are listing the whole of the code, then the JavaScript should be wrapped in a $(document).ready(function(){ ... });.
i.e.
$(document).ready(function(){
$.ajax({
type: "GET",
url: "http://some-rand.om.api.com",
data: "011b38b8-011f-4b03-bb21-4c5bb26600b3",
success: function(msg){alert( msg );}
});
});