I've read dozens of related posts, but I still can't get this to work.
I want to alert the response in jquery I get from PHP.
PHP:
$msg=array();
if(empty($whatever)){
$msg['cenas']="Não há contas";
}else{
$msg['cenas']="Há contas";
};
echo json_encode($msg);
JS:
$.ajax({
url: 'myscript.php',
dataType: 'json',
success: function(response){
alert(response.cenas);
}
});
PHP is echoing
{cenas: "Há contas}"
But I can't get it to alert in JS.
The php should echo back {"cenas": "Há contas"}, but what did you get in the alert? Did you get an undefined? If so, try to use jQuery.parseJSON before alert. e.g:
$.ajax({
url:"myscript.php",
dataType: "json",
success:function(data){
var obj = jQuery.parseJSON(data);
alert(obj.cenas);
}
});
You should tell jQuery to expect (and parse) JSON in the response (although jQuery could guess this correctly...) and you should write your javascript correctly:
$.ajax({
url: 'myscript.php',
dataType: 'json',
success: function(response){
alert(response.cenas);
}
});
Try
$.ajax({
url:"myscript.php",
dataType: "json",
success:function(data){
alert(data.cenas);
}
});
You have a syntax error.
Check out the Docs for $.ajax
Related
So here is my AJAX code using JQuery:
$('#button-upload').on('click', function(){
if( sendSMSArr.length > 0 ){
$.ajax({
url: 'manager/smsendr4.php',
type: 'POST',
dataType: 'json',
data: {'distribution': sendSMSArr},
success: function(response){
}
});
}
});
The request isn't registered in the network.
Secondly, I'm not sure how can I collect this data with PHP using $_POST.
You seem to be a php newbie. Here is a snippet of code you can use to retrive the ajax data.
Here is the link to the documentation about the global variable $_POST I suggest you to read it.
Another useful link about the predefined variables in php
JS code:
$('#button-upload').on('click', function(event){
event.preventDefault();
if( sendSMSArr.length > 0 ){
$.ajax({
url: 'manager/smsendr4.php',
type: 'POST',
dataType: 'json',
data: {'distribution': sendSMSArr},
success: function(response){
console.log(response);
}
});
}
});
PHP:
<?php
if(isset($_POST['distribution'])){
# I've added a sanitization filter, but you can omit it if you don't need to pass the data to a database.
$dist = filter_var($_POST['distribution'], FILTER_SANITIZE_STRING);
# put your logics here after you got the distribution $_POST variable value.
}
?>
I've been having trouble accessing this piece of content in a json object. Here is my code for fetching data:
function getEntries(key){
$.ajax({
url: "https://openlibrary.org/api/books?bibkeys=ISBN:" + key + "&jscmd=details&callback=mycallback",
dataType: "jsonp",
success: function(data){
console.log(data);
}
});
}
The reply I get looks like this:
How do I access the pointed object if the key is different for every search?
Try using
data["ISBN:"+key]
Where key is the key you are passing to the function
I think I found it after all...
function getEntries(key){
$.ajax({
url: "https://openlibrary.org/api/books?bibkeys=ISBN:" + key + "&jscmd=details&callback=mycallback",
dataType: "jsonp",
success: function(data){
console.log(data["ISBN:"+key]);
}
});
}
did the trick.
I have a Ajax-Call inside a Ajax-Call, everything "seems" to work fine. In console I can see, both calls are executed and get a return.
But somehow, i can't use the returned result from the second call(?)
$.ajax({
type: "POST",
url: "register/checkEmail/"+email,
success: function(result){
if(result == "TRUE") {
$('#regMsg').html('Ein User mit dieser Email ist bereits registriert!');
$('#regMsg').slideDown();
// NO ERROR - REGISTER USER
} else {
$('#regMsg').slideUp();
var inputs = $('#regForm :input').serializeArray();
alert('ok');
$.ajax({
method: "POST",
url: "register/save",
data: inputs,
dataType: 'json',
success: function(result){
alert('ddok');
}
});
}
}
});
the first alert() is beeing displayed, the secont is not, although the second call is executed correctly(?) why is that?
Simple - the second call's response did not return back to the ajax i.e error/fail.
Add the error handling part after success to find the response.
After success add
,error: function(result){
alert('error');
console.log(result);
}
If this is not the reason, then dataType: 'json', should be the culprit as your response wouldn't be in json format !!
I have php on my server that is handling the AJAX call and returning a JSON object like so:
$dataArray = array('order_id'=>$order_id, 'response'=>'Sucessfully Added');
header('Content-type: application/json');
echo json_encode( $dataArray );
This is my AJAX call:
$('.add').ajaxForm({url: this.href, type:'post',
data: this.serialize,
dataType: 'json',
success: function(responseText){
var p = JSON.parse(responseText);
alert(p.response);
$('.popupContainer').hide();
}
});
In FireBug I can see that it reaches the line that begins with 'var p' just fine. In fact, at this point, FB tells me that the responseText is exactly what I want it to be: {"order_id":"182","response":"Sucessfully Added"}. But at that point it suddenly stops, so I must be missing something here.
There is no need to do manual parsing, since the dataType is set as json the response will be automatically parsed
$('.add').ajaxForm({
url: this.href,
type: 'post',
data: this.serialize,
dataType: 'json',
success: function (p) {
alert(p.response);
$('.popupContainer').hide();
}
});
jQuery Form
dataType
'json': if dataType == 'json' the server response will be evaluted and
passed to the 'success' callback, if specified
success
responseText or responseXML value (depending on the value of the
dataType option).
I'm trying to get data from a user interface to PHP.
In JS, I have:
var myPostData=JSON.stringify({'categoria':valor,'fluxo':fluxo});
$.ajax({
url:'data.php',
type:'post',
data:myPostData,
dataType: "json",
});
Where valor and fluxo are variables.
And in PHP:
if (isset($_POST['categoria'])){
$fluxo=$_POST['fluxo'];
$categoria=$_POST['categoria'];
echo("Fluxo ".$fluxo);
echo("categoria ".$categoria);
}else{
echo "nada";
}
But I can't get the data to be processed by PHP. I always get 'nada' in return...
Thanks in advance for any help!
Instead of transforming the data to a JSON string, just add it as a plain Javascript object:
var myPostData={'categoria':valor,'fluxo':fluxo};
$.ajax({
url:'data.php',
type:'post',
data:myPostData,
dataType: "json",
});
Also as pointed out in comments, you are accessing the POST variable by $_POST['valor'], when you are sending it as $_POST['categoria'].