Ajax response receiving as XML - php

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

Related

parse HTML table from third party server (jQuery or PHP)

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.

Sending multiple variables in ajax

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.

trouble with json response from php in jQuery.ajax call

I am using $.ajax to get a JSON response from a php script. if i log the data variable from the $.ajax success function it outputs a properly formatted JSON object, however when I try to access properties of the data var it's undefined. here is the php the is being sent back:
echo json_encode(array("status"=>true, "success"=>"Login Success", "message"=>"You have been logged in successfully."));
and here is my ajax call:
$.ajax({
type: "POST",
data: {
login: true,
username: $('#login-username').val(),
password: $('#login-password').val()
},
async: false,
url: "./php/client-login.php",
success: function (data) {
console.log(data.status);
if (data.status) {
console.log(data.success);
displayModal(data.success, data.message, "./js/login-modal-code.js");
} else if (!data.status) {
displayModal(data.error, data.message, "./js/login-modal-code.js");
}
},
error: function (jqXHR, status, responseText) {
console.log(status);
}
});
if i add the dataType: "json" option to the $.ajax call I get a parse error and if i try to do $.parseJSON(data); to access the data in the data var I get and unexpected token error. I'm not really sure what I'm doing wrong, I've used this setup before and it always has worked before but for some reason it isn't now. anyone see where i've gone wrong?
EDIT: forgot to mention here is the response from the php script:
{"status":true,"success":"Login Success","message":"You have been logged in successfully."}
EDIT 2: Here is a screen of my console. the top .length call is the json that was logged from console.log(data) and the bottom one is from the response in chrome dev tools network tab for the response from the php script. they line up perfectly yet the second is showing a length of 93, how can i fix this?
I was reading on jQuery Docs and found that "dataType: jsonp" does not work with sync request, and you're making this kind of request since you have async: false. Turning it on may solve your problem.
Also, give a look at the docs.
Good luck!
So I figured out a workaround for this problem, first I did JSON.stringify on the data followed by JSON.parse and lastly $.parseJSON() to get a javascript object to work with. Not sure why there are 2 invisible characters being added between when it leaves the php script and reaches the $.ajax call so if anyone knows why let me know

jquery message system without php how to?

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.

jQuery.ajax() v1.5 returns "parsererror" for json data

I have this function for getting a server id from a list. The function always returns "parsererror". I have looked at the JSON data returned but I cant seem to get it working, since jQuery have rewritten the ajax in v1.5.
function server_id()
{
$.ajax({
type: "GET",
url: "http://localhost/server_list.php",
dataType: "json",
success: function(data, status) {
alert(status + "\n\n" + data.server_id);
},
complete: function(data, status){
alert(status);
}
});
}
server_list.php
header('Content-type: application/json');
$output['server_id'] = '123';
print json_encode($output);
In firebug Net >> XHR it reads it as JSON as it brings up the tab and the Response tab shows what is below.
{"server_id":"123"}
I have also tried setting the content type header like below but having no luck.
Content-type: application/json
UPDATED
I only get "parsererror" if the validation plugin is loaded from http://bassistance.de/jquery-plugins/jquery-plugin-validation docs.jquery.com/Plugins/Validation v1.7.
If you add the plug jquery automatically adds the jsonp callback to the query string even when you set to false or dont include the parms for jsonp. Very Strange
Any ideas on how to fix?
Thanks
The simple solution here seems to be that jQuery 1.5 is not compatible with 1.7 of the validation plugin. Downgrading to jQuery 1.4.x (or otherwise patching or removing the validation plugin code as philhag suggested) solves the issue.
Huge thanks to those on this thread who identified the conflict. It saved me a bunch of headaches having to debug the jQuery code.
You seem to want regular json communication (dataType is "json" instead of "jsonp" and server_list.php sends json), but you're setting jsonp options. Remove the jsonp and jsonpcallback lines. Setting jsonp to false does not mean you disable it!
When these two lines are commented out, everything seems to work fine.
I suffered for days before finding this thread, thanks to those who pointed at jQuery.validate as the culprit.
In my testing it actually seems to be jquery.validate-vsdoc.js which is causing the issue, not the plugin itself, in case that's of any use to anyone else.

Categories