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.
Related
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 one file with a long script which has a variable "period" in it. I will send the value of that variable via the front page of the site to that file. The value will be filled into the file and return it to the front page.
So I want the code <code period="x"> placed in the front page as something like <code period="1">
I want to do that multiple times with different values.
Does someone know how this can be done?
I saw this script, but it didn't work:
if($error === false) {
alert($error);
$.ajax({
url: '\get.php',
type: 'POST',
dataType: "json",
data: {
period: "1"
},
success: function(data){
alert(JSON.stringify(data));
}
});
Thanks
You have a syntax error. See the inline comment:
if($error === false) {
alert($error);
$.ajax({
url: '/get.php',
type: 'POST',
dataType: "json",
data: {
period: "1"
},
success: function(data){
alert(JSON.stringify(data));
}
});
} // << this was missing
you missed a } for the callback to success.
if($error === false) {
alert($error);
$.ajax({
url: '/get.php',
type: 'POST',
dataType: "json",
data: {
period: "1"
},
success: function(data){
alert(JSON.stringify(data));
}
});
}// **This was missing**
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!");
}
});
});
Here is my script:
$.ajax({
type: "Get",
url: "Sample.js",
datatype: 'json',
data: JSON.stringify({ key:key }),
success: function (data) {
var sample = data.name;
$("#html").html(sample);
},
error: function () {
alert("Error");
}
});
This is my Sample.js file:
{ "name": "user" }
When I run this code I get a blank screen. This is my script using getJSON():
$.getJSON("Sample.js", function (data) {
var sample = data.name;
$("#html").html(sample);
})
This produces "user" perfectly. What is the problem with $.ajax code?
In the getJSON version your don't send any data. Could this be the reason why that works? To me it looks like this could be sth. on the server side that delivers an empty JSON object when you pass the key parameter.
As the jQuery documentation states:
$.ajax({
dataType: "json",
url: url,
data: data,
success: success
});
Try modifying the dataType param.
change your datatype to dataType. Its case sensitive. Refer http://api.jquery.com/jQuery.getJSON/
Remove JSON.Stringify and change Get to GET
$.ajax(
{ type: "GET",
url: "Sample.js",
dataType: "json",
data: {key:key },
success: function (data)
{ var sample = data.name; $("#html").html(sample); },
error: function () { alert("Error"); }}
);
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);
}
});