I am passing JSON data to dependent input field - ajax

GET http://localhost:8000/getguardrate/1?_token=kSrGyMIF7twomtmIShadxWbqhNpUKlrxH6lEwDO6 500 (Internal Server Error)
this is my ajax call
$(document).ready(function() {
$('#select2-3').on('change', function() {
var c_id = $(this).val();
console.log(c_id);
if(c_id) {
$.ajax({
url: 'getguardrate/' +c_id,
type: "GET",
data : {"_token":"{{ csrf_token() }}"},
dataType: "json",
success:function(data) {
// console.log(data);
if(data){
$('#GetsiteguardRate').empty();
$('#GetsiteguardRate').focus;
$("#GetsiteguardRate").val(data[0]['sbr_g']);
}else{
$('#GetsiteguardRate').empty();
}
}
});
}else{
$('#select2-3').empty();
}
});
});

Related

Display all data fetched, api rest using __next

I need to fetch all data from an xml link but I couldn't as it displays only 300 rows, so I found a solution that says I should use __next, I have the code below but it doesn't work, in the console I get the next url but the items (TaskName) of the first page. I want to get the TaskName(s) of the next pages.
window.addEventListener('load',function() {
$.ajax({url: _spPageContextInfo.siteAbsoluteUrl + "/_api/ProjectData/[en-US]/Tasks",
method: "GET",
dataType: "json",
headers: {Accept: "application/json;odata=verbose"},
success: function(data) {
var dataResults = data.d.results;
if (data.d.__next) {
url = data.d.__next;
console.log("url: "+url);
}
$.each(dataResults, function(key, value)
{
var tasky = value.TaskName;
console.log(tasky);
});
}});
});
I found a solution, here the code is:
function GetListItems(){
$.ajax({
url: urly,
method: "GET",
headers: {
"Accept": "application/json; odata=verbose"
},
success: function(data){
response = response.concat(data.d.results);
if (data.d.__next) {
urly = data.d.__next;
GetListItems();
}
$.each(response, function(key, value)
{
var tasky = value.TaskName;
console.log(tasky);
});
},
error: function(error){
}
});
}

Ajax Form Submit with attachment

I have a Form on my Site thats submitted true ajax. This Form has a field where to attache .pdf files. How when submitting the form though the file is not send with the rest of data.
How can i get this to work?
Here is my ajax code:
$('#submit_btn').click(function () {
$.ajax({
type: 'POST',
url: '/contact.php',
dataType: "json",
data: $('#contactform').serialize(),
success: function (data) {
console.log(data.type);
console.log(data.msg);
var nClass = data.type;
var nTxt = data.msg;
$("#notice").attr('class', 'alert alert-' + nClass).text(nTxt).remove('hidden');
//reset fields if success
if (nClass != 'danger') {
$('#contactform input').val('');
$('#contactform textarea').val('');
}
}
});
return false;
});
On the php side i have phpmailer setup and am handling the file so:
if(!empty($_FILES['file'])) {
$_m->addAttachment($_FILES['file']['tmp_name'],$_FILES['file']['name']);
}
$('#submit_btn').click(function () {
var formData = new FormData($('#contactform'));
$.ajax({
type: 'POST',
url: '/contact.php',
// dataType: "json",
data: formData ,
processData: false,
contentType: false,
success: function (data) {
console.log(data.type);
console.log(data.msg);
var nClass = data.type;
var nTxt = data.msg;
$("#notice").attr('class', 'alert alert-' + nClass).text(nTxt).remove('hidden');
//reset fields if success
if (nClass != 'danger') {
$('#contactform input').val('');
$('#contactform textarea').val('');
}
}
});
return false;
});

Call multiple ajax calls

$.ajax({
url: "",
jsonpCallback: 'item',
contentType: "application/json",
dataType: 'jsonp',
success: function(data) {
console.log(data);
var markup = "";
$.each(data.list, function(i, elem) {
dbInsert(elem['itemCode'], elem['description'], elem['price']);
});
},
error: function(request, error) {
alert(error);
}
});
I have the above type of different ajax calls with different urls. How can I run each Ajax call, one after another?
You can do something like this.
$('#button').click(function() {
$.when(
$.ajax({
url: '/echo/html/',
success: function(data) {
alert('one is done')
}
}),
$.ajax({
url: '/echo/html/',
success: function(data) {
alert('two is done')
}
})
).then( function(){
alert('Final Done');
});
});
fiddle
Keep track of the urls still to send, and have the success inline function of one ajax request go and call the next.
var urls = [...];
var runNextAjax = function() {
var url = urls.pop();
$.ajax({
url: url,
... other settings, as before ...
success: function(data) {
... do what you want with the data ...
if (urls.length > 0) runNextAjax();
},
error: function(req, err) {
... do what you want with the error ...
if (urls.length > 0) runNextAjax();
}
});
};
// Start the sequence off.
runNextAjax();
The above code acts on the data as it arrives, if you want to cache it all and act on it all at the end, store each result in an array, then process the array in a function that gets called at the end:
var dataAccumulator = [];
var displayAllData = function() {
for (int i = 0; i < dataAccumulator.length; ++i) {
var data = dataAccumulator[i];
... process the data into HTML as before ...
}
};
var urls = [...];
var runNextAjax = function() {
var url = urls.pop();
$.ajax({
url: url,
... other settings, as before ...
success: function(data) {
// Store the data we received
dataAccumulator.push(data);
if (urls.length > 0) runNextAjax();
else displayAllData();
},
error: function(req, err) {
... do what you want with the error ...
if (urls.length > 0) runNextAjax();
else displayAllData();
}
});
};
// Start the sequence off.
runNextAjax();

asp.net mvc 3 json does not work

This is my jquery with json
$('#btnVerificationOk').click(function () {
var verId = $('#trans_verification_id').val();
var verCode = $('#trans_verification_code').val();
$.ajax({
url: '/Profile/CompleteTransactions',
type: 'POST',
data: { },
dataType: 'json',
success: function (result) {
alert(result);
},
error: function () {
alert('ERROR ERROR !!!!!!!!!!!!');
}
});
});
And My C# method:
[Authorize]
[HttpPost]
private JsonResult CompleteTransactions()
{
return Json("Done");
}
Its always alerts 'ERROR ERROR !!!!!!!!!!!!' i tried debugging but CompleteTransactions method is not firing
And this is my second json which is bellow and works good
$('#btnTransfareOk').click(function () {
var userName = $('#transfare_username').val();
var amount = $('#transfare_amount').val();
if (userName == '' || amount == '') {
$('#transfare_error_list').html('Please fill boxes.');
} else {
$.ajax({
url: '/Profile/TranfareMoney',
type: 'POST',
data: { ToUsername: userName, Amount: amount },
dataType: 'json',
success: function (result) {
$('#transfare_error_list').html(result.text);
$('#trans_verification_id').val(result.id);
$('#transfare_username').val("");
$('#transfare_amount').val("");
},
error: function () {
$('#transfare_error_list').html('Oops... Error.');
}
});
}
});
I'm not 100% sure, but shouldn't you controller action handler be public ?

send parameter with jquery ajax

I've a php page and I would to send a jquery ajax request to another page for sending email. This new page accept one parameter and then it executes query ecc...
<div id="customer" name="customer" style="visibility: hidden;"><?php echo $customer; ?></div>
<?php echo '<input id="pulsante" type="image" src="includes/templates/theme485/buttons/english/button_confirm_order.gif" onclick="inoltra();"> '; ?>
this is the script
function inoltra(){
var jqxhr = $.ajax({
type: "POST",
url: "../gestione_di_canio/services.php",
datatype: "json",
data: ??????????????
async:true,
success:function() {
try{
alert("ok");
}catch (e){
alert("correggi");
}
}
});
}
how can I pass to data value of div "customer"?
On data: { varName: $('#customer').html() }
on Php:
$varName= $_POST['varName'];
For a simpler sintax, you use this (it the same):
$.post("../gestione_di_canio/services.php", { varName: $('#customer').html() } )
.success(function() {
try{
alert("ok");
}catch (e){
alert("correggi");
}
});
function inoltra(){
var jqxhr = $.ajax({
type: "POST",
url: "../gestione_di_canio/services.php",
datatype: "json",
data: { customer: $('#customer').html() },
async:true,
success:function() {
try{
alert("ok");
}catch (e){
alert("correggi");
}
}
});
}
I hope this will work for ya...
function inoltra(){
({var jqxhr = $.ajax
type: "POST",
url: "../gestione_di_canio/services.php",
datatype: "json",
data: {"parameter1": value1, "parameter2": value2},// e.i data: {"customer": $('div#customer').html() },
async:true,
success:function() {
try{
alert("ok");
}
catch (e)
{
alert("correggi");
}
}
});
}
as many parameters you want to send would be accessable.
function inoltra(){
var jqxhr = $.ajax({
type: "POST",
url: "../gestione_di_canio/services.php",
datatype: "json",
data: JSON.stringify({paramName: $('#customer').html()}),
async:true,
success:function() {
try{
alert("ok");
}catch (e){
alert("correggi");
}
}
});
}

Resources