The content that i want to send
<div id="preview">        </div>
Element function:
function _(obj) {
return document.getElementById(obj);
}
Ajax:
$.ajax({
type: 'POST',
url: 'http://<?php echo $domain ?>/libraries/ajax/pdf.php',
data: 'html=' + _("preview").innerHTML + '&nama=a',
dataType: 'html',
beforeSend: function() {},
success: function(response) {
Materialize.toast((response), 4000);
}
});
How to send &nsbp as text instead of POST parameter?
Use the encodeURIComponent(<string to be encoded>) to encode the data
Use
data: { html: _("preview").innerHTML, nama: 'a'}
And $.ajax will encode it. Under the hood, it uses uses this encodeURIComponent(), you use it like
data: 'html='+encodeURIComponent(_("preview").innerHTML)+'&nama=a',
Related
I am making a call with ajax to a php file. All I want to do is get a value back from the php file to test it with javascript. I have tried many many things, but can only get undefined (from the below code).
How can I get "hello" returned from the php file to my javascript?
jQuery.ajax({
type: "POST",
url: "the_file_to_call.php",
data: {userid: group_id_tb},
success: function(data) {
var the_returned_string = jQuery(data).html();
alert(the_returned_string)
}
});
The PHP file:
<?php
echo '<div id="id-query-result>"';
echo "hello";
echo "</div>";
You can change the code inside PHP like this
<?php
$queryResult = '<div id="id-query-result">hello</div>';
echo json_encode(['html' => $queryResult]);
Then, change your ajax call
jQuery.ajax({
type: "GET",
url: "the_file_to_call.php",
data: {userid: group_id_tb},
dataType: 'json',
success: function(data) {
var the_returned_string = data.html;
alert(the_returned_string);
}
});
$.ajax({
type: "POST",
url: "the_file_to_call.php",
data: {userid: group_id_tb},
success: function(data) {
$('body').append(data);
var text = $('#id-query-result').text();
alert(text);
$('#id-query-result').remove()
}
});
Why not just append the HTML response of your php file then get the text accordingly. You can then remove it after.
There are two changes to be done:
Change the type to "GET" since you directly call the PHP File.
remove the wrapped jQuery method inside the success function and add .html as an attribute
jQuery.ajax({
type: "GET",
url: "the_file_to_call.php",
data: {userid: group_id_tb},
success: function(data) {
var the_returned_string = data.html
alert(the_returned_string)
}
});
I want to call a json data inside my AJAX success. I'm still new on manipulating json and AJAX. Can somebody help me on how to access my json data which is from another URL? And I want to compare the id to the JSON data. Here's my code so far:
function getCard(id){
$.ajax({
type: "GET",
data: "id=" + id,
success: function(data){
#call JSON data here from another URL
# Is it possible to call another AJAX here?
}
});
}
This code will work for you
$.ajax({
url: "url",
method: "post",
data: "id=" + id,
success:function(data) {
// success goes here
$.ajax({
url: "url",
async:false,
method: "post",
data: "id=" + id,
success:function(json) {
JSON.parse(json);
// compare data and json here
},
error: function(){
// error code goes here
}
});
},
error: function(){
// error code goes here
}
});
yes Zuma you can call another Ajax inside Success function of Ajax call. Below is the example:
$.ajax({
type: "post",
url: url1,
data: data1,
success: function(data){
$.ajax({
type: "post",
url: url2,
data: data2,
success: function(data){
});
});
});
function getCard(id){
$.ajax({
type: "Get",
url: "发送请求的地址",
data: "id=" + id,
success: function(data){
#call JSON data here from another URL
# if success,you can call another AJAX.
# Is it possible to call another AJAX here?
# yes!
}
});
}
This is how I want to delete a record using jquery ajax
deleteFile: function(obj) {
$.ajax({
type: 'delete',
dataType: 'json',
url: 'service/lead.php?a=deleteFile',
data: {id: $(obj).attr('data-lf-id')}
}).done(function(response) {
console.log("done");
}).fail(function(error) {
});
}
This works but how am I suppsed to get the id value on the lead.php page? This is what I am currently doing but it doesn't capture the id value.
//service/lead.php
if ($_SERVER['REQUEST_METHOD'] == "DELETE") {
if ($_GET['a'] == 'deleteFile') {
echo json_encode($lead->deleteLeadFile($_REQUEST['id']));
}
}
I would suggest changing your Ajax type to either 'POST' or 'GET". You can then use the appropriate $_GET or $_POST variable to retrieve your id.
deleteFile: function(obj) {
$.ajax({
type: 'POST',
dataType: 'json',
url: 'service/lead.php?a=deleteFile',
data: {id: $(obj).attr('data-lf-id')}
}).done(function(response) {
console.log("done");
}).fail(function(error) {
});
}
//service/lead.php
if ($_GET['a'] == 'deleteFile') {
echo json_encode($lead->deleteLeadFile($_POST['id']));
}
file_get_contents(“php://input”)
This always gives you raw request data - you have to parse it [with parse_str if you send urlencoded data, with json_decode if you send JSON].
But you should keep it RESTful and not RPC-like, and issue a delete over /resource/:id.
I have the following span:
<span class='username'> </span>
to populate this i have to get a value from PHP therefor i use Ajax:
$('.username').html(getUsername());
function getUsername(){
$.ajax({
type: 'POST',
url: myBaseUrl + 'Profiles/ajax_getUsername',
dataType: 'json',
data: {
},
success: function(data){
document.write(data);
}
})
}
Now when i debug i see that the returned data (data) is the correct value but the html between the span tags stay the same.
What am i doing wrong?
Little update
I have tried the following:
function getUsername(){
$.ajax({
type: 'POST',
url: myBaseUrl + 'Profiles/ajax_getUsername',
dataType: 'json',
data: {
},
success: function(data){
$('.username').html('RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRr');
}
})
}
getUsername();
Still there is no html between the tags (no text) but when i look at the console the method is completed and has been executed.
Answer to the little update
The error was in my Ajax function i forgot to print the actual response! Thank you for all of your answers, for those of you who are searching for this question here is my Ajax function:
public function ajax_getUsername(){
if ($this->RequestHandler->isAjax())
{
$this->autoLayout = false;
$this->autoRender = false;
$this->layout = 'ajax';
}
print json_encode($this->currentClient['username']);
}
Do note that i am using CakePHP which is why there are some buildin methods. All in all just remember print json_encode($this->currentClient['username']);
The logic flow of your code is not quite correct. An asynchronous function cannot return anything as execution will have moved to the next statement by the time the response is received. Instead, all processing required on the response must be done in the success handler. Try this:
function getUsername() {
$.ajax({
type: 'POST',
url: myBaseUrl + 'Profiles/ajax_getUsername',
dataType: 'json',
data: { },
success: function(data){
$('.username').html(data); // update the HTML here
}
})
}
getUsername();
Replace with this
success: function(data){
$('.username').text(data);
}
In success method you should use something like this:
$(".username").text(data);
You should set the html in callback
function getUsername() {
$.ajax({
type: 'POST',
url: myBaseUrl + 'Profiles/ajax_getUsername',
dataType: 'json',
data: {
},
success: function(data){
$('.username').html(data);
}
})
}
Add a return statement for the function getUsername
var result = "";
$('.username').html(getUsername());
function getUsername(){
$.ajax({
type: 'POST',
url: myBaseUrl + 'Profiles/ajax_getUsername',
dataType: 'json',
data: {
},
success: function(data){
document.write(data);
result = data;
}
})
return result;
}
You can use .load()
Api docs: http://api.jquery.com/load/
In your case:
$('.username').load(myBaseUrl + 'Profiles/ajax_getUsername',
{param1: value1, param2: value2});
I have a javascript variable that is created in php
echo "var".$locallist."=".create_js_variable($locallist,$db2_list_all,'db2');
and it looks in html page source code like
var locallist={
'CARINYA':[['2011-08-24-09-22 - w','20110824092216w'],['2011-08-18-13-15','20110818131546']],
'COVERNAN':[['2011-03-02-12-28','20110302122831']],
'DAVID':[['2010-12-22-19-43','20101222194348'],['2010-12-08-14-10','20101208141035']]};
Now I want to update the variable on button click via ajax
jQuery.ajax({
type: 'get',
dataType: 'text',
url: url,
data: {
what: 'db2list',
t: Math.random()
},
success: function(data, textStatus){
locallist = data;
console.log(locallist);
}
});
and the ajax calls this php code (note that it's the same php function that is called)
if($what == 'db2list') {
$db2_list_all = get_db2_database_list();
echo create_js_variable($locallist,$db2_list_all,'db2');
}
Console.log reports that
before update the variable is an object
after update it is a text
How can I fix that? So my other javascript code works again?
Well, look at your ajax call:
jQuery.ajax({
type: 'get',
dataType: 'text',
url: url,
data: {
what: 'db2list',
t: Math.random()
},
success: function(data, textStatus){
locallist = data;
console.log(locallist);
}
});
Notice you've got the dataType as text? It's going to bring the data in and treat it as text. Try changing the dataType to 'json'. That will convert the data that was brought in to a regular 'ol object, just like what you already have.
Your AJAX-request has a type dataType: 'text',
Change it to JSON :)
Use dataType: 'json'.
jQuery.ajax({
type: 'get',
dataType: 'json',
url: url,
data: {
what: 'db2list',
t: Math.random()
},
success: function(data, textStatus){
locallist = data;
console.log(locallist);
}
});