I have following script
$.ajax({
type:"GET",
url:"views/jquery/js/private/visual_constructor/proxy.php",
data:null,
timeout:55000,
dataType:"xml",
error:function(XMLHttpRequest, textStatus, errorThrown){
alert("error="+XMLHttpRequest+" error2="+textStatus+" error3="+errorThrown);
},
success:function(response){
alert('sucess');
}
});
and the content of the proxy.php is following.
<?php
header('Content-type: application/xml');
echo file_get_contents('http://server.name/somefile.php');
?>
It connects to another server where somefile.php generates some xml content and prints it.
It works perfectly in Chrome, but in Mozilla it shows me my error alert.
What is wrong here?
update 1
I am using firebug and it says that everything is just OK. Even it shows the response from the server. And here is what my error alert prints:
error=[object XMLHttpRequest] error2=parsererror error3=parsererror
update 2
When I open http://server.name/somefile.php from the Mozilla it shows me this message:
XML Parsing Error: not well-formed
Location: http://authoringtool/views/jquery/js/private/visual_constructor/proxy.php
Line Number 8, Column 94: <xs:annotation><xs:documentation xml:lang="en">Network Type</xs:documentation></xs:annotatin>
But again when I open it from Chrome it doesn't show me the error but prints the content of the somefile.php
I think you're doing a cross-server request (more importantly, a cross-domain request ), and as such, you need to do extra security stuff.
https://developer.mozilla.org/En/HTTP_access_control
https://developer.mozilla.org/En/Server-Side_Access_Control
http://www.w3.org/TR/cors/
http://arunranga.com/examples/access-control/
Firebug
Also, with Firebug, debugging is better as objects, then you can inspect the returned objects.
console.log({ "error": XMLHttpRequest, "error2": textStatus, "error3": errorThrown });
Try setting the content type from the query itself:
$.ajax({
type:"GET",
url:"views/jquery/js/private/visual_constructor/proxy.php",
data:null,
timeout:55000,
contentType: "application/xml;charset=UTF-8",
dataType:"xml",
error:function(XMLHttpRequest, textStatus, errorThrown){
alert("error="+XMLHttpRequest+" error2="+textStatus+" error3="+errorThrown);
},
success:function(response){
alert('sucess');
}
});
You open with:
<xs:annotation>
And close with:
</xs:annotatin>
Fix the spelling error and the problem should go away.
Related
This command works great if I call it during page execution
exec('run.exe -something', $response, $status);
But I have a button on the page that executes the same command via AJAX:
// Attempt a call to change the password
$.ajax({
url: 'actions/runcommand.php',
type: 'POST',
data: {
cid: cid,
},
dataType: 'json',
success: function(msg)
{
if (ajaxErr(msg) return;
// Show success message
showMsg('success!');
},
error: function(jqXHR, textStatus, errorThrown){ajaxFail(jqXHR, textStatus, errorThrown)}
});
I've verified the two are executing literally the same string whether I do it in my code or via the AJAX, but the AJAX initiated one is returning an error code instead of functioning. The value of status is 1 which is a generic error so I have little to go on.
I looked around and found one similar post, but they solved it by removing https from the ajax command. If I do that the browser blocks it with scary messages.
This is running on a local machine which attempts HTTPS but fails because it's a self-signed cert or something. I turned off https redirection for the site, but that made zero difference. Not sure what it's unhappy about... please help!
if (ajaxErr(msg) return;
Error handling shouldn't be done in the success callback function. You can remove this or modify what this function is doing.
If you're requesting the URL from a different directory, make sure your URL parameter uses a leading slash for the root directory:
url: '/actions/runcommand.php',
Try this and see if there's still a generic error:
$.ajax({
url: '/actions/runcommand.php',
type: 'POST',
data: {
cid: cid,
},
dataType: 'json',
success: function(data, status) {
console.log(data);
console.log(status);
showMsg('success!');
},
error: function(jqXHR, textStatus, errorThrown) {
ajaxFail(jqXHR, textStatus, errorThrown)
}
});
Ok folks, this had all the signs of a boneheaded mistake and that's what it was. I'm leaving it here for posterity because it's an easy mistake to make and I saw other people ask similar questions with no good answer.
The SOLUTION was that my AJAX files were in a SUBDIRECTORY and that means when they executed the command the output which was trying to export to a subdir of the root was trying to find that subdir in ANOTHER SUBDIR. Sheesh!
Can't work this one out for the life of me, hopefully I am not doing something stupid but why this is not working is not clear to me.
I have a basic HTML page with a JQuery script that sends the following AJAX call to a PHP script within the same directory.
JQuery:
// Sends the AJAX request
$.ajax({
type: "GET",
url: "process.php",
dataType: "json",
success: function(data) {
console.log(data);
}
});
PHP:
<!-- Ajax request handler -->
<?php
echo json_encode(array('message' => 'AJAX call received'));
exit();
?>
The AJAX call is being made successfully as after debugging it in the console it's status code is 200 and statusText 'ok'. However, I simply cannot get the returned JSON message to show up in the console as it should.
I have double checked the URL and that's fine.
This is the response I get in the console using Jeff Hatz's AJAX Debugger Chrome Extension:
Console Screenshot
Any ideas folks?
You need to remove this line from the top of your PHP:
<!-- Ajax request handler -->
As when you make a ajax call with dataType: 'json' it is not going to parse the response (at all) and then when you do a console.log(data); it is simply empty, no console log.
When you do remove that line, you should instead receive in your Network tab Response:
{"message":"AJAX call received"}
Which then in a console.log(data); you should see:
Object {message: "AJAX call received"}
On my website I've got an jquery accordion plugin. In order to collect all form data (from several forms) I'm using this line: $('#form_values').val($('form').serialize()). Then I submit the data to my php file, which in this case contains:
<?php
header('HTTP/1.1 500 Internal Server Booboo');
header('Content-Type: application/json; charset=UTF-8');
return json_encode(array("error" => "bad table name"));
?>
I would expect that my json/ajax/jquery script would display an alert box with "bad table name", but instead it just displays an alertbox with the text: Parseerror.
My jquery script:
$.ajax({
type:'POST',
data:$('#form_values').val($('form').serialize()),
url:'handle.php',
dataType: "json",
success: function(data){
alert("sucesso: "+data.msg);
window.location='index.php';
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert (textStatus);}
});
I haven't got a clue what's going wrong here.. hopefully someone is willing to help me out.
You need to use serializeArray instead of serialize and then convert it to a JSON string:
$.ajax({
type:'POST',
data:JSON.stringify($('form').serializeArray()),
url:'handle.php',
dataType: "json",
success: function(data){
alert("sucesso: "+data.msg);
window.location='index.php';
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert (textStatus);}
});
I think that somehow, the server script does not return json (use firebux/other debugging tools to check the response). As you specified that the response's type should be json, you get a parseerror.
Edit: are you sending the form to the right url? e.g. /handle.php instead of handle.php?
$.ajax({
type: "POST",
url: "check-email",
data: {email: "name#example.com"},
success: function(data)
{
// success part here
},
error: function(xhr)
{
alert(xhr.status);
}
});
This will call another PHP file. But the sad thing is the error part is executed always with xhr.status printing as 0. What would be the problem?
Note:
1. This works perfectly in the local.
2. Both the script, check-email page is located in the same server.
3. The server is secured with https and it is in WWW version.
Update:
error: function(jqXHR, textStatus, errorThrown)
{
alert(textStatus, errorThrown);
}
This prints simply as "error".
I would have done this as a comment but the code would be a nightmare.
In your PHP page write this somewhere:
<?php
echo '<pre>'.print_r($_POST, true).'</pre>';
echo '<pre>'.print_r($_GET, true).'</pre>';
?>
Now:
Open up your Developer tools (Chrome) or Firebug (FireFox) and visit the Network tab.
Refresh your page that makes the AJAX call
Find the AJAX call to "check-email" in the Network tab and click it
What do you see?
Also there is a sub-tab called "Headers" in the developer tools, take a look at it
//MAKE AJAX CALL TO REPOPULATE TABLE
var newData = 'page_number=1&type=2';
$.ajax({
type: 'POST', // HTTP method POST or GET
url: 'http://www.myurl.net/form_validate.php', //Where to make Ajax calls
dataType:'text', // Data type, HTML, json etc.
data:newData, //post variables
success:function(response){
//REFORMAT UPON SUCCESSFUL AJAX CALL
alert(response);
},
error:function (xhr, ajaxOptions, thrownError){
alert(xhr + " " + ajaxOptions + " " + thrownError); //throw any errors
}
});
All I've put in my PHP file is:
<?php echo "test"; ?>
When I go straight to that file, it echoes 'test.' When I try to run the AJAX function on the click of a button it gives me the error:
[object Object] error
in an alert window. I've put the absolute URL to the file because I was thinking that the relative linking I was using was wrong but it now seems like it's some other issue. Am I overlooking a simple syntax error? Sorry if this is super basic but I can't seem to figure this out after working on it for a really long time. Thanks for your help.
The problem is your absolute url . Some browsers have problems dealing with absolute urls, consider it as a cross-domain request and block it even if it's not a cross-domain request. You should try with relative url instead
The problem might be in the url, try with relative path instead of absolute.
You named the file was in the same folder as the .js file, so try
url: '/directory_path_to_the_same_folder_as_the_JS_file/form_validate.php',
Try using done with jQuery.post instead.
For example:
jQuery.post('form_validate.php', data).done(
function(data, textStatus, jqXHR) {
alert(jqXHR.responseText);
}).fail(function(jqXHR, textStatus, errorThrown) {
alert(jqXHR.responseText);
}).complete(function() {
// Common code when request completes
});
If your error is [object Object] error NOT FOUND then the reason is
the specified url in AJAX is not correct.
If your error is [object Object] error INTERNAL SERVER ERROR , its
because of the error in your server file ie the php file ,error like variables not defined correctly etc..
Some times error may occur due to cross domain, if you have not
specified headers when in case your php file is not in the same
domain
Hope this helps
Thank you
function abc(){
var ret=true;
$.ajax({
type: 'POST',
url: 'send_password.php',
data: 'mail_to=mymail&new_password=pwd',
async:false,
success: function(response){
},
error: function(r){
ret=false;
}
});
return ret;
}
alert(abc());
Have a look at this testfiddle