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.
Related
I have ajax that calls a php file called "fetch_quarter_limit.php" from template file.
$.ajax({
type: 'POST',
url: 'fetch_quarter_limit.php',
data: {
leavefrom: from,
leaveto: to,
empid: emp
},
success: function(data) {
var quarter_limit = data['myVar'];
alert(quarter_limit);
}
});
In my .php file i have tried to return the session data as an array.
Fetched the required data, stored in session and formed an array.
$_SESSION['quarter_limit_mend'] = $quarterLimit;
$returnVal = array('myVar' => $_SESSION['quarter_limit_mend']);
echo $returnVal;
As shown in above ajax code part, i tried to fetch it, but all i am getting is "undefined" when i output the variable using alert.
Please help.
Ajax code updated, p2 :
Adding dataType is making code not to work.
$.ajax({
type: 'POST',
url: 'fetch_quarter_limit.php',
dataType: 'json',
data: {
leavefrom: from,
leaveto: to,
empid: emp
},
success: function(data) {
alert(data);
}
});
As #Tim mentioned i have added custom json encode function to my .php file.
It returns as expected {"myVar": 2}
echo array_to_json($returnVal);
This is returned from php file.
But not able to access in ajax code.
You're using echo on an array, which is not possible. As described in the PHP manual
echo outputs one or more strings.
Usually you'd use json_encode() on your array and then output it to the screen. But as you've commented you are using php < 5. First of all, if possible, you should consider to upgrade to PHP > 7, as this not only improves performance, it also improves security.
If you can't upgrade to a PHP version above PHP 5, then you can use workarounds. On this question there is already an answer for the workaround, and the workaround can be found on the PHP manual itself.
You should be able to use the returned JSON data.
Reply for after your edit
So you have your data as JSON now, but JS will still see it as a string. In your success function you still have to parse it.
success: function(data) {
jsonData = JSON.parse(data);
alert(jsonData.myVar);
}
I do have to add too that it is very insecure to continue using php < 7.3 (as of today, 24-02-2021)
I have a HTML table with sport results on a third party server that I would like to parse as a JSON or XML so I can grab me the values out of it...
I would prefer to do this with jQuery and already played around with $.ajax but I don't get it running :/
I also thought about a PHP script running on my server and doing something with file_get_contents() and parsing the result as JSON - without success...
Dose anyone have an idea - what is the best solution to do what I want? I need a thought-provoking impulse ;)
My jQuery attempt:
$.ajax({
dataType: "jsonp",
url: "....",
success: function(data) {
console.log(data);
},
error: function() {
console.log('error');
}
});
Running in to an error:
As per comments:
For cross-site content, make a proxy script using PHP. All this will need to do is grab the remote content and echo it out.
Your ajax request will then point to this script instead, and you will be able to parse the response using standard jQuery functions as if it were a normal page e.g. $(data).filter('table');
Your 'dataType' is wrong? Shouldn't that be either 'html' or 'json' since i see you are receiving HTML content.
I have the following ajax request:
var value = $.ajax({
type: "POST",
url: "url.php",
data: { $(someform).serialize(), something: test_number },
cache: false,
async: true
}).success(function(data){
alert("success!");
}).error(function() {
console.log("FAILED");
});
But it is logging FAILED although the url is right. What happens is that the page refreshes the page and the php query isn't done. I guess there are no errors within the url... any idea on why this happens?
You are kind of mixing methods to send your POST data. You can't serialize a query strong and then also append additional data to it using javascript object construct. You will likely need to manually append the last data element to the query string like this:
data: $(someform).serialize() + '&something=' + encodeURIComponent(test_number),
Of course there could still be a problem on the server-side script which is causing a non-200 HTTP response code (and triggering error handler). You just need to fix this first, and if you still have a problem, debug the server-side issue.
I'm getting all ajax responses as XML only. When i implement them, it worked fine(i got the responses as HTML). Is there any reason that we are receiving the responses as XML by default? I think something has been changed recently. But i couldn't able to corner the change. Any help on this will be greatly appreciated.
NOTE: Please note that I'm using jQuery for AJAX.
Here is the code i'm using in one of the place in my site (in all the places i'm using in the same manner and it is coming in XML only, as shown in the screenshot)
$.ajax({
type: "POST",
url: "/ajax_contests_submissions_more&popup=yes",
dataType:"html",
data: 'last_pos='+queryPos,
cache: false,
error:function(XMLHttpRequest, textStatus, errorThrown) {
alert('sorry, we were unable to process your request. please try again later');
},
success: function(html)
{
}
});
Please check this image which shows that browser will not guess the XHTML as XML as per #dystroy's comment.
AJAX replies tries to parse the response as XML. So you can grab either XML or the raw text.
See this
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/