I have a php function that get some xml information to be handled, but my problem is to pass a parameter to the function in addition to the data returned from the ajax :
$.ajax({
type: "POST",
url: 'proxy.php',
data:{country:'home'},
dataType: "xml",
success: function(data)
{
alert(data);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest);
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}
});
I want to pass parameter with the data .
I know that I could do it like the following:
$.ajax({
type: "POST",
url: 'proxy.php',
data:{country:'home'},
dataType: "xml",
success: function do_it(parameter)
});
function do_it(parameter)
{
return function(data)
{
alert(parameter);
alert(data);
}
}
I do not want to use this way I want to pass the parameter as the first code is showing.
Is it possible?
You can use the context option to $.ajax. This will then be available as this in the callback.
$.ajax({
type: "POST",
url: 'proxy.php',
data:{country:'home'},
dataType: "xml",
context: parameter,
success: function(data)
{
alert(data);
alert(this);
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest);
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}
});
If your parameter is not affected by the AJAX call, simply define it in the same scope as the call and then access it from the success function. For example:
var parameter = 'some value';
$.ajax({
type: 'post',
url: 'proxy.php',
data: {country: 'home'},
dataType: 'xml',
success: function (data) {
alert(parameter);
alert(data);
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest);
alert(textStatus);
alert(errorThrown);
alert(XMLHttpRequest.responseText);
}
});
This works because parameter is in an outer scope and thus accessible by the success function.
Related
For Facebook login I'm using this code
FB.api('/me', {fields: 'birthday,cover,devices,email,first_name,gender,id,last_name,link,location,name,name_format,timezone,verified,website,locale'}, function(response) {
$.ajax({
url: '/login/facebook',
type: 'POST',
data: { fb: response, window: window.ui},
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (xhr, ajaxOptions, thrownError){
notice(xhr);
notice(ajaxOptions);
}
});
});
If i'm calling without {fields: '...'} its working but when added fields ajax sending GET request to server instead of post, how to get response with desired fields from FB.API and post it to server?
I fixed this problem with little change in code instead of url: '/login/facebook', I wrote url: '/login/facebook/', just / at the end and problem solved !
Did you try?
FB.api('/me', 'post', {fields: 'birthday,cover,devices,email,first_name,gender,id,last_name,link,location,name,name_format,timezone,verified,website,locale'}, function(response) {
$.ajax({
url: '/login/facebook',
type: 'POST',
data: { fb: response, window: window.ui},
dataType: 'json',
success: function (data) {
console.log(data);
},
error: function (xhr, ajaxOptions, thrownError){
notice(xhr);
notice(ajaxOptions);
}
});
});
I have two checkboxes with some values like this:
<label for="hotel_boutique"><input id="hotel_boutique" name="hotel_boutique" value="test" type=checkbox />test</label><br />
3
and I get these values with an Ajax call like this:
<script>
jQuery("input[type='checkbox']").change(function(){
if (jQuery('input#hotel_boutique').is(':checked')) {
var hotel_boutique = jQuery("#hotel_boutique").map(function () {return this.value;}).get();
}else{
var hotel_boutique = 'NULL';
}
if (jQuery('input#hotel_stars').is(':checked')) {
var hotel_stars = jQuery("#hotel_stars").map(function () {return this.value;}).get();
}else{
var hotel_stars = 'NULL';
}
var data = 'hotel_boutique="'+hotel_boutique+'"&hotel_stars="'+hotel_stars+'"';
jQuery.ajax({
url: "processAjax.php",
type: "GET",
data: data,
cache: false,
beforeSend: function() {
jQuery("#loading").show();
},
success: function(data, textStatus, XMLHttpRequest){
jQuery("#content").html('');
jQuery("#content").append(data);
jQuery("#loading").hide();
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}
});
});
</script>
Now when I echo the variables in the server side (PHP) it displays:
"test"
Who and why are the " added? And how can I remove them?
I have already tried the PHP function preg_replace and other stuff.
please need your help...
You can use an object instead of string like
var data = {
hotel_boutique: hotel_boutique,
hotel_stars: hotel_stars
};
The problem is the " in the data string, you can also try
var data = 'hotel_boutique='+hotel_boutique+'&hotel_stars='+hotel_stars;
The overall code can be simplified as
jQuery("input[type='checkbox']").change(function () {
var data = {
hotel_boutique: $('#hotel_boutique:checked').val()||'NULL',
hotel_stars: $('#hotel_stars:checked').val()||'NULL'
};
jQuery.ajax({
url: "processAjax.php",
type: "GET",
data: data,
cache: false,
beforeSend: function () {
jQuery("#loading").show();
},
success: function (data, textStatus, XMLHttpRequest) {
jQuery("#content").html(data);
jQuery("#loading").hide();
},
error: function (MLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
});
you need to pass key-value pair:
data: {hotel_boutique: hotel_boutique,hotel_stars: hotel_stars},
In this call:
jQuery.ajax({
data: data, // change here
});
it should be :
jQuery.ajax({
url: "processAjax.php",
type: "GET",
data: {
hotel_boutique: hotel_boutique,
hotel_stars : hotel_stars
},
cache: false,
beforeSend: function() {
jQuery("#loading").show();
},
success: function(data, textStatus, XMLHttpRequest){
jQuery("#content").html('');
jQuery("#content").append(data);
jQuery("#loading").hide();
},
error: function(MLHttpRequest, textStatus, errorThrown){
alert(errorThrown);
}
});
I have a problem:
I have a JS function which sending data to php script, then PHP script returning JSON data from database QUERY and I want to get values returned from PHP script.
<script type="text/javascript">
<!--
jQuery('#wysz2').submit(function() {
var myData = {
"rodzaj_konta": jQuery('#rodzaj_konta').val(),
"miejscowosc": jQuery('#miejscowosc').val()
};
jQuery.ajax({
url: 'http://somescript.php?action=results',
type: 'GET',
data: myData,
dataType: 'json',
beforeSend: function() {
jQuery('#loading').html('<p>loading...</p><img src="loading.gif" />'); //Loading image during the Ajax Request
},
error: function(xhr, textStatus, errorThrown) {
alert("Error: " + (errorThrown ? errorThrown : xhr.status));
},
contentType: "application/json; charset=utf-8",
success: function(data) {
alert(data);
}
});
return false;
});
//-->
</script>
The PHP script returning data in proper format using:
header('Content-Type: application/json');
echo json_encode($data);
When I'm trying to alert(data), I get always a null.
How to get this returned JSON data ?
EDITED:
It's strange, because I have changed sending method to POST.
PHP returning JSON:
[{"nazwa":"Test","nazwa_firmy":"Testowa","ulica":null,"numer_domy":"2A","numer_mieszkania":"11","kod_pocztowy":"00-189","miejscowosc":"Warszawa","telefon":"213-123-132","nip":"112-312-31-31","regon":"231232133","adres_www":"http:\/\/www.gogl.epl","rodzaj_uzytkownika":"serwis"}]
But my JQUERY AJAX Script still returning null.
So my script now looks like this:
<script type="text/javascript">
<!--
jQuery('#wysz2').submit(function() {
var myData = {
rodzaj_konta: jQuery('#rodzaj_konta').val(),
miejscowosc: jQuery('#miejscowosc').val()
};
jQuery.ajax({
url: 'http://somedomain.com/skrypt.php?action=wyniki_wyszukiwania',
type: 'GET',
data: myData,
dataType: 'json',
contentType: "application/json; charset=utf-8",
jsonp: "jsoncallback",
beforeSend: function() {
jQuery('#loading').html('<p>ładowanie...</p><img src="loading.gif" />');//Loading image during the Ajax Request
},
error: function (xhr, textStatus, errorThrown) {
alert("Error: " + (errorThrown ? errorThrown : xhr.status));
},
success: function (data) {
alert(JSON.stringify(data));
console.log(data);
}
});
return false;
});
//-->
</script>
Any ideas ?
you are constructing your variables while sending in a wrong way semicoluns for object names is not there according to definitions
try this
var myData = {
rodzaj_konta: jQuery('#rodzaj_konta').val(),
miejscowosc: jQuery('#miejscowosc').val()
};
and while alerting your json data try
alert(JSON.stringify(your_json_obj));
Try to alert the object of the result...
Means if json in the format {"responseCode":"001","responseMsg":"success"}
Then alert data.responseCode
In success of your ajax function try something like this
var objParams1 = $.parseJSON(data);
console.log(objParams1);
alert(objParams1.Testowa)
ajax
$('#stb_no').blur(function(){
var stb_no= $('#stb_no').val();
$.ajax({
url: "http://localhost/paymybill/ajax/stb_info",
global: false,
type: "POST",
data: {
'stb_no':stb_no, // you should give a key to the variable
},
success: function(data) {
$('#amount').val(data);
// $(".email_msg").addClass("red");
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
Controller code
public function stb_info(){
$stb_no=$this->mso->alldata_stbno($this->input->post('stb_no'));
echo json_encode($stb_no);
}
i am getting out put
[{"sxb_no":"xxxxxx","mzo_name":"xx","cto_name":"xxxxx","area":"xxxxx","name_sxb_owr":"","mobile_no":"xxxxxx","email":"xxxxx#yahoo.com","amount":"xxx"}]
i need to know how to get each values ex:- if i want get email id what i should i do in jquery please help me i new to ajax
Most browsers support JSON.parse(), which is defined in ECMA-262 and is the recommended way. Its usage is simple (I will use your example JSON):
var json = '{"area":"xxxxx",...,"email":"xxxxx#yahoo.com","amount":"xxx"}';
var obj = JSON.parse(json);
Note that obj.email can't be used, because you are parsing an array.
Edit: Checking your comments you need to know that the data parameter is the JSON object, parse it first and then you can do:
$('#amount').val(obj[0].email);
Just add to $.ajax call parameter dataType:"json" and Jquery will parse it in success parameter automatically. Then use it data[0]['email'];
For example :
$('#stb_no').blur(function(){
var stb_no= $('#stb_no').val();
$.ajax({
url: "http://localhost/paymybill/ajax/stb_info",
global: false,
type: "POST",
data: {
'stb_no':stb_no, // you should give a key to the variable
},
success: function(data) {
$('#email').val(data[0]['email']);
//OR
var obj = JSON.parse(data);
$('#email').val(obj[0].email);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
});
I am not sure why but the post to my external PHP file is not working. The post request is not being received by the PHP file as nothing is being outputted.
Here's my jQUery;
$.post("AJAX/get_track_info.php", { url: "uploads/19c9aa51c821952c81be46ca9b2e9056.mp3"}, function(info){
$('#loadInfo').html(info);
});
And in the PHP file is just a
$trackurl = $_POST['url'];
Try something more like this
$.ajax({
type: 'POST',
url: 'AJAX/get_track_info.php',
data: { url: "uploads/19c9aa51c821952c81be46ca9b2e9056.mp3" },
dataType: 'text',
success: function (data, textStatus, jqXHR) { },
error: function (data, textStatus, errorThrown) { }
});