Retrieve data from PHP via Ajax / jQuery - javascript

I need help with my code, how can I get the value bar into the .table (for example . thx
My php file foo.php
<?php
header('Content-Type: application/json');
echo json_encode(array('foo' => 'bar'));
?>
My jQuery code
$(function () {
$('button#click').click(function(e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'foo.php',
data: {data.foo},
dataType : 'json',
success : function (data) { $('.table').html("<p>"+data.foo+"</p>"); }
});
});
});

$(function () {
$('button#click').click(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'foo.php',
data: {},
dataType: 'json',
success: function (data) {
$('.table').html("<p>" + data.foo + "</p>");
}
});
});
});

You can use it another way
$(function () {
$('button#click').click(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: 'foo.php',
data: 'key1=val1&key2=val2',
success: function (data) {
result = eval('(' +data + ')')
$('.table').html("<p>" + result.foo + "</p>");
}
});
});
});
And php file :
<?php
echo json_encode(array('foo' => 'bar'));
?>

Related

Wordpress ajax request return 0

Hello I have a problem with wordpress I can not get an ajax call and I can not find the reason. My query returns me all the time 0.
my javascript code :
updateButton.onclick = function (e) {
var donne = {
'action': 'my_action',
'lodges': updateDeleteArray
};
$(function () {
$.ajax({
type: "POST",
data: donne,
url: ajaxurl,
contentType: "application/json",
dataType: 'json',
success: function (data) {
console.log(data);
},
});
});
};
my php code :
add_action('wp_ajax_my_action', 'my_action');
add_action('wp_ajax_nopriv_my_action', 'my_action');
function my_action() {
echo 'salut';
die();
}
Try deleting contentType and dataType, because you are not returning a JSON into your function.
Or
modify your function like this
function my_action() {
echo json_encode('salut');
die();
}
hope it helps

Problems with $_POST["var"] in Yii Controller

i am having problems with $_POST["var"] on controller. It seems to be empty. How can i receive the string being typed on my textFiled?
View
<?php Yii::app()->clientScript->registerCoreScript("jquery"); ?>
<script type="text/javascript">
$(document).ready(function(){
$("#hhmm").change(function(){
$.ajax({
url: "<?php echo CController::createUrl('reqTest01Loading'); ?>",
data: $(this).serialize(),
type: "post",
dataType: "json",
success: function(data) {
if (data.status === 'failure') {
$('#impatto').val('Error request failed.');
} else {
$("#impatto").html(data.total);
}
}
});
});
});
</script>
Controller
public function actionReqTest01Loading() {
$result = array("total" => $_POST['hhmm'], "status"=>"OK");
echo CJSON::encode($result);
}
rules on Controller
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('reqTest01Loading','index','view','admin'),
'users'=>array('#'),
),
thanks in advance
This is because you dont tell ajax call to send parameters.
Try:
$.ajax({
url: "<?php echo CController::createUrl('reqTest01Loading'); ?>",
data: {data:$(this).serialize()},
type: "post",
dataType: "json",
success: function(data) {
if (data.status === 'failure') {
$('#impatto').val('Error request failed.');
} else {
$("#impatto").html(data.total);
}
}
});
And it controller:
public function actionReqTest01Loading() {
// var_dump(Yii::app()->request->getPost('data'));
$result = array("total" => Yii::app()->request->getPost('data'), "status"=>"OK");
echo CJSON::encode($result);
}

How can i redirect page JavaScript/PHP?

I want to redirect page to URL that return from PHP script.
That when user created a post i want to return value of mysql_insert_id() for post_id.
$('input#submitButton').click( function() {
$.ajax({
url: 'newpost.php',
type: 'post',
dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
window.location.replace("http://localhost/?post_id=...");
}
});
});
I looked here but not found what i want.
Link
In your php script do :
echo json_encode(array(
'id' => $THE_ID
));
then here do:
$('input#submitButton').click( function() {
$.ajax({
url: 'newpost.php',
type: 'post',
dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
window.location = "http://localhost/?post_id=" + data.id;
}
});
});
});

Passing GET parameter with ajax

I have a link that I want to use with ajax. Here is the link:
<a class="export_csv" href="ajax/createCSV.php?saleid=4"><img src="/img/record.csv.png"></a>
The ajax works fine but I can't pass through the GET variable. Here is the jquery:
$('.export_csv').on('click', function(e){
e.preventDefault();
$.ajax({
url: 'ajax/createCVS.php',
type: 'GET',
data: $(e).data['saleid'],
success: function(results){
console.log('it worked');
}
});
});
Here is the target php page:
<?php
include('./includes/global.php');
//$cl = new Client();
//$cl->createCSV();
echo "This Works ";
$test = $_GET['saleid'];
echo $test;
echo "did work for me";
?>
try like this ,send data to php page using data option
$('.export_csv').on('click', function(e){
e.preventDefault();
$.ajax({
url: 'ajax/createCVS.php',
type: 'GET',
data: "saleid=4",
success: function(results){
console.log('it worked');
}
});
})
or
$('.export_csv').on('click', function(e){
urls=$(this).attr('href');
e.preventDefault();
$.ajax({
url:urls,
type: 'GET',
success: function(results){
console.log('it worked');
}
});
}
You need to pass the data as JSON format like
data:{saleid:$(e).data['saleid']}
But actually dont know what is $(e).data['saleid']
$('#myDomSelectorId').data['saleid'] need to be JSON formated like this :
data : { saleid : $('#myDomSelectorId').data['saleid'] }
Or directly data : "saleid="+$('#myDomSelectorId').data['saleid']
Full example :
$('.export_csv').on('click', function(e){
e.preventDefault();
$.ajax({
url: 'ajax/createCVS.php',
type: 'GET',
data: { saleid : $('#myDomSelectorId').data['saleid'] },
success: function(results){
console.log('it worked');
}
});
});

JSP AJAX return NULL

i using JQuery Ajax to call REST api in jsp but it return null no matter how i call but it can work in html. is there any way to solve this problem. Cant seem to find a solution in the net.
$(document).ready(function () {
alert('ready');
var accessKey = 'xkg8VRu6Ol+gMH+SUamkRIEB7fKzhwMvfMo/2U8UJcFhdvR4yN1GutmUIA3A6r3LDhot215OVVkZvNRzjl28TNUZgYFSswOi';
var thisUrl = 'http://www.onemap.sg/API/services.svc/getToken?accessKEY=' + accessKey;
$.ajax({
type: "GET",
url: thisUrl,
dataType: 'application/json',
success: function (data) {
alert('data is:' + data.GetToken[0].NewToken);
}
});
alert(thisUrl);
});
dataType should be jsonp
$(document).ready(function () {
var thisUrl = 'http://www.onemap.sg/API/services.svc/getToken?accessKEY=' + accessKey;
$.ajax({
type: "GET",
url: thisUrl,
dataType: 'jsonp',
success: function (data) {
console.log(data)
alert('data is:' + data.GetToken[0].NewToken);
}
});
});
Refer to this article:
http://www.isgoodstuff.com/2012/07/22/cross-domain-xml-using-jquery/
You only need "jquery.xdomainajax.js" that is there in the sample source-code to make it work.
$.ajax({
url: 'https://asdf/asdf',
dataType: "xml",
type: 'GET',
success: function(res) {
var myXML = res.responseText;
alert(myXML);
}
});

Categories

Resources