I've tried almost all suggestions but it doesn't work. My ids are working properly in the JavaScript function when I alert my array using arr['id']. However, when I try $_POST['id'] on a different PHP file (I've used AJAX and specified the URL) is gives me an error.
scriptfile.php:
<script>
function detailsmodal(cid, pid) {
var arr = { "cid": cid, "pid": pid };
jQuery.ajax({
url: '/frontend/include/detailsmodal.php',
method: "post",
data: arr,
success: function(data) {
jQuery('body').append(data);
jQuery('#details-modal').modal('toggle');
},
error: function() {
alert("something went wrong");
}
});
}
detailsmodal.php
<?php
echo $_POST['cid'];
?>
You could try sending your data as json like this:
$.ajax({
type: "POST",
url: "/frontend/include/detailsmodal.php",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({ cid: cid, pid: pid }),
success: function(data) {
jQuery('body').append(data);
jQuery('#details-modal').modal('toggle');
},
error: function() {
alert("something went wrong");
}
});
For the decoding you could use javascript JSON.parse
var myObj = JSON.parse(this.responseText);
or
$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data["cid"];
Related
I've been trying to send this information to the js.
$statement = ibase_prepare($sql);
$query = ibase_execute($statement);
while ($row = ibase_fetch_assoc($query)) {
$data[] = $row;
}
echo json_encode($data);
However, when I try to, I get the response, but I can't get the data.
$('document').ready(function (){
$.ajax({
method: "POST",
url: "../Dashboard/php/chart.php",
dataType: "json",
sucess: function (data){
console.log(JSON.stringify(data));
}
});
})
You just have a grammatical error in a word "sucess", the correct word is "success"
$.ajax({
method: "POST",
url: "./data.php",
dataType: "json",
sucess: function (data){ // Here -> success
alert(JSON.stringify(data));
}
});
I am trying to execute the AJAX operation. the call is working fine but the problem I am having is that I am getting an empty string as a response. There is no error in the console. all I get is an empty string. Even when I change the dataType to JSON, I still get the same response.
JavaScript Code:
$.ajax({
url: "data/saveCart.php",
method: "POST",
data: {
cartItem:item
},
dataType: "text",
success: function(data){
console.log(data);
}
});
PHP code:
if(isset($_POST['cartItem'])) {
echo "AJAX successful";
} else {
echo "AJAX failed";
}
It seems like it was caused by not stringifying your data.
var item = {
toothbrush: {
price: 1
}
};
$.ajax({
url: "data/saveCart.php",
method: "POST",
data: {
cartItem: JSON.stringify( item )
},
dataType: "text",
success: function(data){
console.log(data);
}
});
here is my code in ajax
function loadcountries()
{
var p = document.getElementById("selectCntry");
while(p.firstChild)
{
p.removeChild(p.firstChild);
}
var data = {
action: "loadccc"
};
jQuery.ajax
(
{
type: "POST",
url: "ajax-ows2.php",
dataType: 'json',
async:false,
data:data,
success: function(msg)
{
alert(msg.test);
}
}
);
}
here is my ajax-ows2.php
<?php
$action = $_POST["action"];
include "dbconnect.php";
if($action == "loadccc")
{
$var = $action;
$response_array['test'] = $var;
header('Content-type: application/json');
echo json_encode($response_array);
}
?>
and here is my function call:
<script>
window.onload = loadcountries;
</script>
my ajax way is different. I really have no idea why it is not alerting when the page is load. Im really sure that my ajax-ows2.php is good and im sure that my function call is correct. Can somebody help me with this. This is not a duplicate one. I tried to used asynch:false but still not working.
try this format:
$.ajax({
method: "POST",
contentType: "application/json; charset=utf-8",
data: data,
url: 'ajax-ows2.php',
success: function (data) {
console.log(data);
},
error: function (error){
console.log(error);
}
});
since you are doing POST method, your data parameter must be a stringify, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
I try to do ajax and then back json data in Wordpress.
My Ajax is:
$.ajax({
type: "POST",
dataType : 'html',
url: "/wp-content/themes/myproject/ajax/otros_alojamientos.php",
data: {
idPost: '<?php echo $ID; ?>'
},
success: (function(data) {
alert(data); // this is my json
var a = $.parseJSON(data);
alert(a.titulo); // there is not nothing here. Undefined.
}),
error: (function(data) {
console.log('Error alojamientos');
}),
});
My Json back is:
string(50) "{"titulo":"aaatitle","descripcion":"bbbdescription"}"
I can not print titulo or descripcion with alert or console.log...
Cheers!
change your datatype param
$.ajax({
type: "POST",
dataType : 'json',
url: "/wp-content/themes/myproject/ajax/otros_alojamientos.php",
data: {
idPost: '<?php echo $ID; ?>'
},
success: (function(data) {
alert(data.titulo); // data is parsed as Json by jQuery
}),
error: (function(data) {
console.log('Error alojamientos');
}),
});
you should check your error callback function params also (it doesn't expect data as 1st param)
Well the response you are getting that is neither a valid json or valid html. So either you remove the dataType and then you have to parse it or just add dataType:"json" but in your case that is not a valid json.
So i would like to tell you that you should use dataType:"text" and then you can parse it:
$.ajax({
type: "POST",
dataType : 'text',
url: "/wp-content/themes/myproject/ajax/otros_alojamientos.php",
data: {
idPost: '<?php echo $ID; ?>'
},
success: (function(data) {
var json = data.split(' ')[1]; // gets you the json string
var validJson = $.parseJSON(json); // parses as valid json
alert(validJson.titulo);
}),
error: (function(data) {
console.log('Error alojamientos');
}),
});
Here is my script:
$.ajax({
type: "Get",
url: "Sample.js",
datatype: 'json',
data: JSON.stringify({ key:key }),
success: function (data) {
var sample = data.name;
$("#html").html(sample);
},
error: function () {
alert("Error");
}
});
This is my Sample.js file:
{ "name": "user" }
When I run this code I get a blank screen. This is my script using getJSON():
$.getJSON("Sample.js", function (data) {
var sample = data.name;
$("#html").html(sample);
})
This produces "user" perfectly. What is the problem with $.ajax code?
In the getJSON version your don't send any data. Could this be the reason why that works? To me it looks like this could be sth. on the server side that delivers an empty JSON object when you pass the key parameter.
As the jQuery documentation states:
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
Try modifying the dataType param.
change your datatype to dataType. Its case sensitive. Refer http://api.jquery.com/jQuery.getJSON/
Remove JSON.Stringify and change Get to GET
$.ajax(
{ type: "GET",
url: "Sample.js",
dataType: "json",
data: {key:key },
success: function (data)
{ var sample = data.name; $("#html").html(sample); },
error: function () { alert("Error"); }}
);