I am learning json now.
$.ajax({
async: true,
type: "POST",
url: "get.....values.asp",
data: "vendorId="+vendor,
success: function(json){
alert( "Data retrieved: " + json );
}
});
I am using this ajax call to get the data as json and data is coming as fallowing:
{"rows": [
{"cell":[
104,100,140,"2.99",0.1,1,14,123.55
]
}
]}
How can i retrieve the data from this json object?
can any one give Idea?
thanks in advance.
$.parseJSON(json) will do the work.
Have you tried...
json.rows[0].cell[0]
...etc.?
I also notice you haven't specific the datatype when calling your $.ajax function, e.g.
$.ajax({
async: true,
type: "POST",
url: "get.....values.asp",
data: "vendorId="+vendor,
dataType: 'json',
success: mySuccessHandler
});
Try running it through a for loop in the callback function and assign variables to the bits of information you want to use.
Related
I get the following error on the .php page. Notice: Undefined index: name
$.ajax({
url: 'test.php',
type: "GET",
data: ({name: "James"}),
success: function(data){
console.log(data);
}
});
I am trying to get the value sent from the data object in the test.php file as follows:
if(isset($_GET['name'])){
echo $_GET['name'];
} else {
echo "Not working";
}
Try this :
data: {name: "James"},
Instead of
data: ({name: "James"}),
You can use serialize for accommodate your data
example :
var serializedData = 'name=James';
$.post('test.php', serializedData,
function (enumData) {
console.log(enumData);
}
);
hope this helps you. cheers :)
I suspect it's something wrong with the jQuery part. Try to make the data a valid JSON object data: {"name": "James"} (notice how "name" is enclosed in quotes):
$.ajax({
url: "test.php",
type: "POST",
data: {"name": "James"},
success: function(data){
console.log(data);
}
});
Edit: Removed dataType. As Taplar pointed out, this is linked to the returned data.
I have a variable which contains data, i'm then using a ajax function to send this variable data to this php file. I'm slightly unsure I can store this variable into php and echo it out. This is the code that I currently have...
var data = 1
// Sending this data via ajax to php file/
$.ajax({
type: 'post',
cache: false ,
url: 'function.php',
data: data,
success: function(data) {
alert ( data );
}
});
This is my php code
$noteone = $_POST['data'];
echo $noteone;
Any help would greatly be appreciated
data of your ajax call should be like below. Hope it will solve your problem.
data: { "data": data }
You also need to set the data type:
var data = 1
// Sending this data via ajax to php file/
$.ajax({
type: 'post',
cache: false ,
url: 'function.php',
data: { "data": data },
dataType: "json", // <---- THIS ONE
success: function(data) {
alert ( data );
}
});
Try this :
var data = 1
// Sending this data via ajax to php file/
$.ajax({
type: 'post',
cache: false ,
url: 'function.php',
data: JSON.stringify(data),
contentType: "application/json",
success: function(data) {
alert ( data );
}
});
PHP requires data to be submitted in key=value format when building $_POST/$_GET. You didn't do that. You only submitted value, so PHP has no key to populate $_POST with. You need to have:
data: { "whatever_you_want": data }
which becomes
$_POST['whatever_you_want']
How to pass the fetched data using $.get() from a URL to a particular URL with query_string that can be retrieved using $_GET() in PHP. I'm using following but not working. I want to pass the fetched data into following parameter http://example.com/data?data=FETECHED_DATA
<?php
$ip_address=$_SERVER['REMOTE_ADDR'];
echo '<script>
$.get( "http://ipinfo.io/'.$ip_address.'/org", function( data ) {
$.ajax({
type: "GET",
url: "http://example.com/data",
data: data,
success: success,
dataType: dataType
});
});
</script>';?>
Well, don't recommended PHP to fetch and send data
I believe this will do what you want
<?php
$ip_address=$_SERVER['REMOTE_ADDR'];
echo '<script>
$.get("http://ipinfo.io/'.$ip_address.'/org", function(data) {
$.ajax({
type: "GET",
url: "http://example.com/data",
data: {data : data},
success: success,
dataType: dataType
});
});
</script>';?>
the only change is data: data changed to data: {data: data}
that's a lot of data :p
see this cut down mock up, http://jsfiddle.net/cm8o5dk8/ if you have a decent browser that can show you the network "usage" you'll see that the mockup fails trying to GET http://example.com/data?data=AS15169+Google+Inc.%0A - the %0A comes back from ipinfo.io
Thanks it worked
$.get("http://ipinfo.io/8.8.8.8/org", function(data) {
$.ajax({
type: "GET",
url: "http://example.com/data",
data: {data : data},
success: function() {},
dataType: 'json'
});
});
Following is the code that Ii have used to get the response in json.
but when I add alert alert(response.Subject); it returns "undefined"
HTML:
<input type="text" id="subject" value='Subject'>
Javascript:
$.ajax({
url: "/ebays/prefilledcontentajax",
type: "POST",
cache: false,
async: true,
data: {
Type: $("#Type").val(),
},
success: function (response) {
console.log(response); // it show the json that response returns. I want to show in the input box prefilled with the data that response return
}
});
please help me out.
You can use val to set value in the textbox
$('#subject').val(response[0].Subject);
Also, you might want to change the ajax call:
$.ajax({
url: "/ebays/prefilledcontentajax",
type: "POST",
cache: false,
async: true,
data: {
Type: $("#Type").val(),
},
dataType: "json",
// ^^^^^^^^^^^^^
success: function (response) {
// response is JSON
$('#subject').val(response[0].Subject);
}
});
val
Set the value of each element in the set of matched elements.
http://api.jquery.com/val/#val2
ajax
Perform an asynchronous HTTP (Ajax) request.
http://api.jquery.com/jQuery.ajax/
It really depends the construction of your JSON object, but standart is responseText that you parse with JS
maybe you try to get the response text or response JSON
$.ajax({
url: "/ebays/prefilledcontentajax",
type: "POST",
cache: false,
async: true,
data: {
Type: $("#Type").val(),
},
success: function (response) {
console.log(JSON.parse(response.responseText));
//or
console.log(JSON.parse(response.responseJSON));
}
});
you can write like this:
$('#subject').val(response.Subject);
I thinks it's really easy. Just add:
$('#subject').val(response.Subject);
in your success handler.
I have this url which brings back the yahoo time...im guessing the PST
so i need to get this value with javascript...here is my code
$j.ajax({
type: "GET",
url: "http://developer.yahooapis.com/TimeService/V1/getTime?appid=YahooDemo&output=json",
dataType: "jsonp",
complete: function(data){
console.log(data);
}
});
but i cant seem to pull out that Timestamp out of the json...what am I doing wrong
You're using the complete method, which returns the XHR object, not the result.
You want success:
$j.ajax({
type: "GET",
url: "http://developer.yahooapis.com/TimeService/V1/getTime?appid=YahooDemo&output=json",
dataType: "jsonp",
success: function(data){
console.log(data.Response.Timestamp);
}
});
Source: http://api.jquery.com/jQuery.ajax/
I think you want to use the success callback:
$j.ajax({
type: "GET",
url: "http://developer.yahooapis.com/TimeService/V1/getTime?appid=YahooDemo&output=json",
dataType: "jsonp",
success: function(data,status,xhr){
console.log(data.Result.Timestamp);
}
});
The JSON looks like {"Result":{"Timestamp":1331089290}}. That is, an object property called Result, which is another object literal containing the property Timestamp:
// Use .success rather than .complete
success: function(data){
console.log(data.Result.Timestamp);
}
javascript:
//change
dataType: "jsonp",
//to
dataType: "json",
Then extract the timestamp with data.Result.Timestamp.
When you use the value, remember that UNIX timestamp is in seconds whilst the javascript Date object works in milliseconds.