get JSON data from a web-service to make a picture gallery - javascript

A web-service is available at http://www.cs.sun.ac.za/rw334/products.php?
limit=12&skip=0
I want to get the data in there into a Javascript array. I've searched around and I think I should start like this?
$.ajax({
type: "GET",
url: "http://www.cs.sun.ac.za/rw334/products.php?limit=12&skip=0",
data: {id:id, name:name,url:url},
contentType: "application/json",
dataType: "json",
success: ??
}
});
How should I continue to get this data from the .php file into a Javascript array?

product = [];
$.ajax({
type: "GET",
url: "http://www.cs.sun.ac.za/rw334/products.php?limit=12&skip=0",
dataType: "json",
success: function(data) {
$(data.products).each(function(i, products){
product[products.id] = products.name;
});
console.log(product);
}
});

Related

Converting $.post into $.ajax

I have a post statement,
$.post("panel.php", 'data=[{"action":"UserInfo"}]', function (userInfo){
//processing
});
I need it to be converted to $.ajax so made it thus,
$.ajax({
type: "POST",
url: "panel.php",
data: { data: [{"action":"UserInfo"}]},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(userInfo) {
//processing
}
});
But the post variable isn't being sent. Is this not the correct way?
Can you try something like this:
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
success: function(userInfo) {
//processing
}
});
Try this
$.ajax({
type: "POST",
url: "panel.php",
data: "action="+"UserInfo",
success: function(userInfo) {
//processing
}
});
Remove data from your data and keep it in a variable, stringify before your send as below
var data={"action":"UserInfo"};
$.ajax({
type: "POST",
url: "panel.php",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(userInfo) {
//processing
}
});
Your data attribute was not written as correct JSON:
data: { "data": [{"action":"UserInfo"}]},
You need quotation marks around the items inside your JSON object. You can use JSONLint to check if your JSON object is valid.
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
dataType: "json",
success: function(userInfo) {
//processing
}
});
Need a small change. there is a predefined format to send data in ajax,
data: {status: status, name: name},
data: "status="+status+"&name="+name.
Follow any one of the approach.
try like this,
$.ajax({
type: "POST",
url: "panel.php",
data: {"action":"UserInfo"},
success: function(userInfo) {
}
});

Saving data to the database via JQuery .ajax

I am attempting to save 2 pieces of information to the database via JQuery using the .ajax function. I want to save the postID and the userID in WordPress. I think I get the jist of most of the function but need to figure out how to send an array to the processing page so I can insert it. This is what i have written so far:
$(document).ready(function() {
$('#saveme').click(function() {
$.ajax({
type: "POST",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: "",
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});
Can anyone shed some light one what goes into the data value to store the 2 pieces of information that I can send to the processing page?
I am using PHP and the code is in a .js file so I might need to also know to send the information over to the js file. Thanks!!
The type of Data to be sent to the server is JavaScript object containing zero or more key-value pairs or String. For your case
data: {
'postID' : $('#postID').val(),
'userID' : $(('#userID').val()
},
data should be a JSON object containing the data you want to save, i.e.
$(document).ready(function() {
$('#saveme').click(function() {
$.ajax({
type: "POST",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: {postID: "A123456", userId: "HGSADKJ"},
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});
Just create a JSON object and send it:
(assuming say there is an element on the page by the id of postID and userID
var jsonData = { "postID" : $("#postID").val(),
"userID" : $(("#userID").va;() }
$(document).ready(function() {
$('#saveme').click(function() {
$.ajax({
type: "POST",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: jsonData,
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});
You forgot to add data
$(document).ready(function() {
var postID='<?php the_id(); ?>';
var userId='<?php author_id(); ?>';
$('#saveme').click(function() {
$.ajax({
type: "post",
url: "save_data.php",
contentType: "application/json; charset=utf-8",
data: {postID: postID, userId: userId},
dataType: "json",
success: function (msg) {
alert("This recipe has been saved in your profile area!");
}
});
});

AJAX POST JSON value

I have AJAX POST, the result is JSON:
$.ajax({
type: "POST",
url: "../../api/test",
data: JSON.stringify(source),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
var upload = JSON.stringify(result);
console.log(upload);
}
});
The upload result is:
{"Link":0,"Title":"d","Description":"dada","Keywords":"dad"}
How can I get the value of Title?
Do not stringify the result, just use result.Title.
As you already have JSON string, It's simple as a pie!
All you need to do is to call the property you want from the variable you assigned your result to.
for example:
var post_response;
$.ajax({
type: "POST",
url: "../../api/test",
data: JSON.stringify(source),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
post_response = JSON.stringify(result);
console.log("Title: "+post_response.Title);
}
});
hope this helps.

getting xml post data in a function called with ajax

I have
$.ajax({
type: "POST",
dataType: "xml",
url: getUrl('/GetPeriodicStats/'),
data: XML.innerHTML,
success: function(c)
{
I need to get XML.innerHTML in GetPeriodicStats.
I know it's supposed to be in $_POST variable, but what index do I write for it ?
I tried $_POST['data'], but it's not good...
What should I write in $_POST[???] ?
Try this
$.ajax({
type: "POST",
dataType: "xml",
url: getUrl('/GetPeriodicStats/'),
data: {xml: XML.innerHTML},
success: function(c)
{
Then that data should be available in $_POST['xml']

Parse List of Entities using Json In Javascript + MVC3

I have looked for a solution , but nothing that fits my needs was found on the site, so here goes:
I have a Controller that returns a Json:
return Json(new { Item = searchModule});
searchModule is a list of Profiles:
{ "Item":[{"ProfileID":4854,"NickName":"Johnny","users":null,"FirstName":"John","LastName":"Doe"}]}
In JavaScript we have:
$.ajax({
type: "POST",
url: action/controller,
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data.Item)
}
})
That returns an object. How can I obtain: Firstname,LastName and NickName ???
Additional answer: If I write the code like below:
var request = $.ajax({
type: "POST",
url: action/controller,
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
}
}).responseText
var obj = json.Parse(request)
, request is null.
since they're objects structured according to the JSON, you should be able to just access the properties like so: data.Item[0].Firstname. You may or may not need to use jQuery.parseJSON to get to this step - calling that is trivial.

Categories