I am developing an HTML/Javascript/JQuery page retrieving a JSON returned by a PHP script:
$.ajax({
url: "data.php",
dataType: 'json',
success: function(rate) {
var retr = JSON.stringify(rate, null, 2);
$("#data").text(retr);
},
error: function(request,error) {
alert(request.statusText);
}
});
When I view the HTML page from my local PC, the Ajax returns the PHP script code rather than the JSON the PHP code should generate. Somehow, it makes sense since it does not go through a PHP engine.
I have tried replacing url: "data.php" with url: "http://www.mysite.com/data.php" or url: "www.mysite.com/data.php", but it does not work.
I would like to avoid uploading my page on a server to test it. I would like to be able to invoke the PHP on my PHP server directly from my local PC. Is this possible?
you will have to upload the page on the server, otherwise you will be restricted by CORS
add the following line in your php script
<?php
header('Access-Control-Allow-Origin: *');
echo json_encode("hello");
?>
I think this post would help you solve your issue just go through the steps mentioned here.
http://php.about.com/od/troubleshooting/qt/shows_code.htm
Take care if the php file has properly set php tag
Related
I'm trying to call a function in javascript to write to a text file. This function uses ajax to to run some code in a php file. All my code seems to be working fine, but I havent gotten any output (If the file doesn't exist, I want it to be created when the php code runs). Everything is being run locally and must be run in chrome. I've found some other people with similar questions, but I wasn't able to apply those solutions. I am very new so please be explicit. Also, if there is a better way to do this (get the system timestamp and write/append to a text file) I am more than open. I just need to record the timestamp whenever a particular javascript function is called. In addition, I would like to eventually pass a string from my running javascript to be saved with each timestamp (the string is determined by the running javascript). Note that running in "Data: running" is a dummy variable. Eventually this will be used to pass a string.
im using this script to use ajax (in html script)
script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js" type="text/javascript"
javascript code..... call recordTone().... javascript code
function recordTone() {
$.ajax({
url: "file:///C:/Users/ryan/Desktop/New%20folder/saveTimeStamp.php",
type: "POST",
data: running,
success: function (msg) {
alert(msg);
}
});
}
saveTImeStamp1.php in entirety
<?php
$my_file = 'session1.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file: '$my_file);
$data = microtime();
fwrite($handle, $data);
fclose($handle);
echo "YES";
?>
You need a web server and then have a URL to it. Easy to install Apache and PHP Like "Ed Heal" said...
or you can use web service to call you procedure/function ...
function recordTone() {
$.ajax({
url: "localhost/yousite/saveTimeStamp.php",
type: "POST",
data: running,
success: function(msg){
alert(msg);
}
});
}
look at 'url', change to your local site...
You need a web server and then have a URL to it. Easy to install Apache and PHP
Not
file:///C:/Users/ryan/Desktop/New%20folder/saveTimeStamp.php
As this will not work
Install XAMPP (http://www.apachefriends.org/en/xampp-windows.html), and then reference your file relatively. If the html file is in C:/Users/ryan/Desktop/New%20folder/, just change the url to "saveTimeStamp.php".
Another option if you don't want to install your own webserver is to purchase some hosting online, from somewhere like http://www.arvixe.com/.
This is really bizarre. I am trying to submit data to the server and have a PHP script parse the data and then send back a response. A correctly formed URL is being sent:
http://localhost/ajax/test.html?row=rec_no_1
, but the server does not seem to respond with content from the PHP script despite a return code of 200. In fact, Developer Tools (in Google Chrome) doesn't say anything about the PHP file. The AJAX "data" statement must be formatted properly otherwise I wouldn't get the correct URL. POST (instead of GET) doesn't help.
If I change the AJAX data to a string, then it works fine. This implies there is something wrong with the AJAX data. But I can't understand what given that the URL is correctly formed and does change depending on which row I select.
Any ideas?
Here is the Javascript:
$(document).ready(function() {
$(".submit").click(function() {
$.ajax({
type: 'GET',
url: 'getTable.php',
dataType: 'html',
data: {row: $('input[type='checkbox']:checked').val()},
//data: {row: 'rec_no_2'},
success: function($result) {
$('.tableHolder').text($result);
}
});
return false;
});
});
Here is the PHP code:
<?php
if (isset($_GET['row'])) {
$tableRow = $_GET['row'];
echo $tableRow;
}
else
echo 'TEST';
?>
Your syntax is incorrect:
$('input[type='checkbox']:checked').val()
You should use double quotes around checkbox:
$('input[type="checkbox"]:checked').val()
Anyway do a console.log( $('input[type="checkbox"]:checked').val() ) before the ajax call just to find out which value is being sent.
Try moving your javascript file to server. Ajax don't work on cross domain. You are running the javascript file on localhost and trying to fetch information from live server. Keep both php and javascript file on same server, either live or localhost. It will work fine.
One more thing, try changing the url parameter of your ajax request. try complete url or try putting or not putting a slash / before your filename.
I am making an ajax call to a php file, But I dont want it to return anything.
My php file has HTML content or rather a JS code, which needs to be executed over there itself. Please help me with this. I dont Know why this is happening
This is my ajax code:
$.ajax({
type : "POST",
url : "<?php echo CALLBACKURL; ?>mobile_profile.php",
data : data,
success: function(response){
}
});
I am not giving any console.log but syill in the response its giving the whole of html
code.
I need to execute that js code there itself and I dont want anything as response Or if we cant do this atleast can i run the JS code return nothing
Where do you want your code to execute? In the browser or at the server?
If you do not want it to contact server and run there itself(inside browser), why are you using AJAX? You simply call a function which will do whatever you want.
on success what do you expect it to do? There is nothing inside the braces...
success: function(response){
}
Browsers cannot process php code. they can process only html and javascripts and some third party scripts with the help of add-ons.
Kindly be clear what you are expecting.
url: "<?php echo CALLBACKURL; ?>mobile_profile.php"
replace it with:
url: "mobile_profile.php"
and put those php tags inside that mobile_profile.php, if required.
If you wish, you can pass parameters to that php file by suffixing your? and a querystring so that the called php file understands what to do.
I think this helps.
The client website needs to do a cross-domain JQuery Ajax call to a php file on my server, the php file will query the database for a bunch of stored javascripts which then need to be sent back to the client and be executed on the client's website. This is what i have so far, haven't done the grabbing javascript from database yet and it works. Is this the best way to do this (assuming i can grab the javascripts directly from the database without adding the escape sequence when echo'ing back to the client)? Thanks.
This is what i have so far:
client side:
$.ajax({ url: "http://localhost:8888/test.php",
dataType: "script",
});
server side (test.php):
<?php
echo "alert(\"WORKS!\");";
?>
Review the ajax documentation and handle the success callback option on the ajax method:
$.ajax({
url: "http://localhost:8888/test.php",
dataType: "html",
success : function(data) { alert(data); }
});
As noted by Ricardo, your PHP script should echo HTML or some other content appropriate for your scenario.
See http://api.jquery.com/jQuery.get/
And http://api.jquery.com/jQuery.getJSON/
i took some interest in this script http://www.9lessons.info/2009/06/comment-system-with-jquery-ajax-and-php.html
and i see that the ajax calls commentajax.php.
what i want to do is to ignore that php, because i want to post to a json file and then get the response from the same file.
my server will use POST or PUT to put the data in the database, so there is no need for me to use php, just the syntax is killing me :)
i want to use :
$.ajax({
type: "POST",
url: "http://www.xxx.com/json",
data: dataString,
cache: false,
success: function(html){
$("ol#update").append(html);
$("ol#update li:last").fadeIn("slow");
document.getElementById('comment').value='';
$("#name").focus();
$("#flash").hide();
}
});
but then how would the commentajax.php look like?
maybe replace the php with :
$.getJSON('http://www.xxx.com/json' , function(data) { ... });
any idea helps
Thanks.
edit1: i have the server-side script in place
If you have the server side scripting set up already, then what is the question again?
If you're asking how to handle the ajax call, then it's mostly a matter of looping through the JSON that you get back, and applying those values to the site in some manner. Pseudo code:
$.getJSON('http://www.xxx.com/json' , function(data) {
for(i=0; i<data.comment.length; i++) {
$(".commentTitle").html(data.comment[i].title);
$(".commentBody").html(data.comment[i].text);
}
});
If I am reading this correctly:
because i want to post to a json file and then get the response from the same file.
You are going to need some server side scripting in order to 'post' to the json file. How are you getting the data into the file.
You can 'read' the data file from the server, that's not a problem, it is a matter of getting the data into the file that you need the server side scripting for.