I'm trying to make a PHP connection, but I keep getting this error. I am hoping someone can help.
My code gives the following error:
{
"readyState": 0,
"status": 0,
"statusText": "NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost/php/busca.php'."
}
My code is:
SendSQL.onclick = function() {
var dataString='a=hello&b=world';
$.ajax({
type: "POST",
url:"http://localhost/php/busca.php",
data: dataString,
async: false,
dataType:'html',
success: function(data){
alert(data);
},
error: function(data){
alert(JSON.stringify(data)); //Better diagnostics
}
});
};
And the file busca.php is:
<?php
$a = $_POST['a'];
$b = $_POST['b'];
echo "$a, $b";
?>
Try this approach...
SendSQL.onclick = function() {
var dataString='a=hello&b=world';
$.ajax({
type: "POST",
url:"http://localhost/php/busca.php",
data: {
"a":"hello",
"b":"world"
},
async: false,
dataType:'text',
success: function(data){
alert(data);
},
error: function(data){
console.log(data);
}
});
};
Your dataString is the way for sending parameter of GET request.
Let's change to JSON like
var dataString = { "a":"hello", "b":"world" };
$.ajax({
type: "POST",
url:"http://localhost/php/busca.php",
data: {data: JSON.stringify(dataString)},
async: false,
dataType:'json',
success: function(data){
alert(data);
},
error: function(data){
alert(JSON.stringify(data)); //Better diagnostics
}
});
And in php code, use json_decode($_POST['data'])
Related
my code is this:
$.ajax({
type: 'Get',
url: 'https://kf.kobotoolbox.org/assets/a7GG9SXYtLTCS4FLm4mjjP/submissions/?
format=json',
dataType : 'jsonp',
success: function(data) {
console.log(data);
},
error: function(data) {
console.log("error");
}
});
When I enter the url on web browser I got the data
but I can not access with ajax
I run the following js:
$(".save_post").on("click", function() {
var saveId = $(".my_post_id").attr("data-id");
console.log(saveId);
$.ajax({
url: "save.php",
data: saveId,
dataType: 'json',
type: 'POST',
success: function(json_object) {
console.log(json_object);
$(".save_post").text("Data has been saved.");
},
error: function(json_object) {
console.log(json_object);
$(".save_post").text("Failed to save data !");
}
});
});
console.log(saveId); gives 248 which is what I would like to save.
PHP page
$post_data = $_POST['data'];
if (!empty($post_data)) {
$file = fopen('data_save.json', 'w+');
fwrite($file, json_encode($post_data));
fclose($file);
echo json_encode('success');
}
var_dump($post_data);
I get if I go to save.php i get the following and the button text after I click says Failed to save data !
Null
Made it work by removing dataType: 'json',
Try this
$(".save_post").on("click", function() {
var saveId = $(".my_post_id").attr("data-id");
console.log(saveId);
$.ajax({
url: "save.php",
data: {saveId:saveId},
dataType: 'json',
type: 'POST',
success: function(json_object) {
console.log(json_object);
$(".save_post").text("Data has been saved.");
},
error: function(json_object) {
console.log(json_object);
$(".save_post").text("Failed to save data !");
}
});
});
Data should be passed in ajax like {data:data}
showing this error: XHR failed to load: AJAX
This is ajax code:
$.ajax({
url:"<?php echo base_url();?>food/register_user/",
type:"ajax",
traditional:true,
data:{data},
dataType:"json",
success:function(data){
console.log(data);
alert(data);
},
error: function() {
alert("Error");
}
});
Try this
$.ajax({
url:"<?php echo base_url();?>food/register_user/",
type:"post",
traditional:true,
data:{data},
dataType:"json",
success:function(data){
console.log(data);
alert(data);
},
error: function() {
alert("Error");
} });
I think the closing braces on the error function is the problem it should look like this
$.ajax({
url: "<?php echo base_url();?>food/register_user/",
type: "ajax",
traditional: true,
data: {
data
},
dataType: "json",
success: function(data) {
console.log(data);
alert(data);
},
error: function() {
alert("Error");
}
});
I Am trying to send value from ajax to php and retrieve it just to test that everything is work, when i click in a button to test i got error and alert('Failed') Appears , how can i fix it in order to get success? thanks
Ajax :
var a = "test";
$.ajax({
url: "search.php",
dataType: "json",
data: a ,
success: function(data) {
alert('Successfully');
},
error: function(data) {
alert('Failed');
}
})
PHP :
<?php
$pictures = "img1";
echo json_encode($pictures);
?>
I refined your code slightly and it works.
var a = "test";
$.ajax({
type: 'POST',
url: 'search.php',
data: 'a=' + a,
dataType: 'json',
cache: false,
success: function (result) {
alert('Successful');
},
error: function (result) {
alert('Failed');
}
});
If you're requesting a JSON, use the $.getJSON from jQuery, it's aready parse the JSON into a JSON object for you.
Seems that you're not return an actual JSON from server, maybe this is what is causing the error.
If you're seeing the 'Failed' message probably the problem is a 500 error which is a server error.
Try this code above.
Javascript:
var a = "test";
$.getJSON("search.php", {
a: a
}, function (json) {
console.log(json);
});
PHP:
<?php
$pictures = ["img1"];
echo json_encode($pictures);
The only way to this not work, is if you have a huge mistake on you webserver configuration.
Your ajax is wrong, it should be:
var a = "test";
$.ajax({
type: "POST",
url: "search.php",
dataType: "json",
data: {a:a},
success: function(data) {
alert('Successfully');
},
error: function(data) {
alert('Failed');
}
});
Is it possible to make an ajax request inside another ajax request?
because I need some data from first ajax request to make the next ajax request.
First I'm using Google Maps API to get LAT & LNG, after that I use that LAT & LNG to request Instagram API (search based location).
Once again, is this possible, and if so how?
$('input#search').click(function(e) {
e.preventDefault();
var source = $('select[name=state] option:selected').text()+' '+$('select[name=city] option:selected').text()+' '+$('select[name=area] option:selected').text();
var source = source.replace(/ /g, '+');
if(working == false) {
working = true;
$(this).replaceWith('<span id="big_loading"></span>');
$.ajax({
type:'POST',
url:'/killtime_local/ajax/location/maps.json',
dataType:'json',
cache: false,
data:'via=ajax&address='+source,
success:function(results) {
// this is where i get the latlng
}
});
} else {
alert('please, be patient!');
}
});
Here is an example:
$.ajax({
type: "post",
url: "ajax/example.php",
data: 'page=' + btn_page,
success: function (data) {
var a = data; // This line shows error.
$.ajax({
type: "post",
url: "example.php",
data: 'page=' + a,
success: function (data) {
}
});
}
});
Call second ajax from 'complete'
Here is the example
var dt='';
$.ajax({
type: "post",
url: "ajax/example.php",
data: 'page='+btn_page,
success: function(data){
dt=data;
/*Do something*/
},
complete:function(){
$.ajax({
var a=dt; // This line shows error.
type: "post",
url: "example.php",
data: 'page='+a,
success: function(data){
/*do some thing in second function*/
},
});
}
});
This is just an example. You may like to customize it as per your requirement.
$.ajax({
url: 'ajax/test1.html',
success: function(data1) {
alert('Request 1 was performed.');
$.ajax({
type: 'POST',
url: url,
data: data1, //pass data1 to second request
success: successHandler, // handler if second request succeeds
dataType: dataType
});
}
});
For more details : see this
$.ajax({
url: "<?php echo site_url('upToWeb/ajax_edit/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function (data) {
if (data.web == 0) {
if (confirm('Data product upToWeb ?')) {
$.ajax({
url: "<?php echo site_url('upToWeb/set_web/')?>/" + data.id_item,
type: "post",
dataType: "json",
data: {web: 1},
success: function (respons) {
location.href = location.pathname;
},
error: function (xhr, ajaxOptions, thrownError) { // Ketika terjadi error
alert(xhr.responseText); // munculkan alert
}
});
}
}
else {
if (confirm('Data product DownFromWeb ?')) {
$.ajax({
url: "<?php echo site_url('upToWeb/set_web/')?>/" + data.id_item,
type: "post",
dataType: "json",
data: {web: 0},
success: function (respons) {
location.href = location.pathname;
},
error: function (xhr, ajaxOptions, thrownError) { // Ketika terjadi error
alert(xhr.responseText); // munculkan alert
}
});
}
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert('Error get data from ajax');
}
});