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!");
}
});
});
Related
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);
}
});
I want to pass the parameter to another page using ajax.Actually i have one popup dialog box,in that dialog box i have one text field,i have to send that value to another page to save into db.not getting how to do it.
Here is my code
$(function() {
$("#button").click(function() {
$("#popup").dialog({
title: "Add",
width: 430,
height: 250,
modal: true,
buttons: {
Add: function() {
var t = ($('#user').val());
$.ajax({
type: "POST",
url: "Details.aspx.cs/getData",
data: {
"test1": t
},
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data);
}
});
$(this).dialog('close');
}
}
});
});
})
The ajax call seems fine. There could be a possibility that the json string is not formed correctly from the textbox javascript value. try using JSON.stringify:
function() {
var t = ($('#user').val().trim());
var payload = { "test1" : t };
$.ajax({
type: "POST",
url: "Details.aspx.cs/getData",
data: JSON.stringify(payload),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
alert(data);
}
});
You are not transferring the data correct. if you want to do it this way, you have you use JSON.stringify of JSON.parse.
You can also try to use
data: "{'test':'" + t+ "'}",
or something of this sort, I used to do it in the past, but don't have the example in front of my eyes now. Will have it clearly later on though.
I have a webform which has x number of textboxes and y number of dropdowns etc
I am using this code to send data to webmethod at the server:
$.ajax({
type: "POST",
url: "SupplierMaster.aspx/RegisterSupplier",
data: JSON.stringify({
id: $('#txtbidderid').val(),
bidamt: $('#txtbidamt').val()
}),
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function (data, status) {
alert(data.d);
},
failure: function (data) {
alert(data.d);
},
error: function (data) {
alert(data.d);
}
});
Now the problem is that I also want to include file attachments on this form.
How do I add the files to data: of $.ajax method?
I do not want to use external plugins etc unless absolutely necessary.
Lets say I modify my data object to look like this :
var dataToSend = {};
dataToSend.id = $('#txtbidderid').val()
dataToSend.bidamt = $('#txtbidamt').val()
dataToSend.append( 'file', input.files[0] );
What would the webmethod armument look like?
For example lets suppose it looks like this as of now:
[WebMethod] public static string SubmitBid(string id, string bidamt.....)
You can try something like this. You may need to manipulate content type.
var dataToSend = new FormData();
dataToSend.append( 'file', input.files[0] );
$.ajax({
url: "SupplierMaster.aspx/RegisterSupplier",
data: dataToSend,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
You cannot send file as application/json; charset=utf-8 to the server and so i suggest you to use application/x-www-form-urlencoded as contentType and also data as FormData as below.
$.ajax({
url: "SupplierMaster.aspx/RegisterSupplier",
type: 'POST',
data: new FormData(formElement),//Give your form element here
contentType: false,
processData: false,
success: function () {
//do success
}
});
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.
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.