I am trying to get the currency from selected country name but ajax returns the value as undefined.
Ajax Code
$(document).on('change', '#country' ,function () {
var prod_id=$(this).val();
console.log(prod_id);
var a=$(this).parent();
var op="";
$.ajax({
type:'get',
url:'{!!URL::to('searchCurrency')!!}',
data:{'id':prod_id},
dataType:'json',//return data will be json
success:function(data){
console.log(data.currency);
// here price is column name in products table data.coln name
a.find('#currency').val(data.currency);
},
error:function(){}
});
});
$.ajaxSetup({ headers: { 'csrftoken' : '{{ csrf_token() }}' } });
Code in controller
$currency=Currency::select('currency')->where('country','=',$request->id)->pluck('currency')->first();
return response()->json($currency);
You should take a look at the request output in your development tools. The result of your query is a single string, so this is also what will be returned from your ajax request. There are no properties.
To get the currency, just use the data variable in your JavaScript:
success: function (data) {
a.find('#currency').val(data);
},
The reason you get undefined as result is because String has no property currency.
Related
I have a button and when it's clicked, I basically want to load the Auth::user()->name and a date into the database. I can't figure out how to do an ajax call without passing in some sort of data. This is my ajax query.
var url2 = "/application-background-sent/{{$application->id}}";
$(document).ready(function(){
$('#bgSubmit').click(function(e){
$.ajax({
url: url2,
success: function(data) {
alert(data);
},
type: 'GET'
});
});
});
My route:
Route::get('/application-background-sent/{id}', 'ApplicationsController#backgroundSent');
My controller:
public function backgroundSent($id)
{
$application = Application::findOrFail($id);
$application->bg_check_submitted_by = Auth::user()->name;
$application->save();
return response()->json(['success'=>'Data is successfully added']);
}
I have an ajax response that is equal to 1. And,in my success function, I want to display not the 1 to my view but the corresponding column_name which is 'brands_name' in the brands table which is 'Apple' in this case. I want to display this after a successful post request. Any ideas?
You need to send your response from your controller (json)
// controller
$data['ajax_response'] = 1;
$data['brands_name'] = $brands_name;
return json_encode($data);
You need to catch the success function to get the variable you want from php
// javascript
$.ajax({
url:link,
dataType:"json",
data:data,
type:"post",
success: function(data)
{
if(data.ajax_response == 1)
{
console.log(data.brands_name);
}
},
error: function()
{
}
});
whenever I send a post request to 'tasks/add' i want the user to return to a new page, but all I get is plain html text in a popup.
Route.php code
Route::post('tasks/add', function() {
return view('secrets');
});
this is my ajax request :
$("#frm").submit(function(e){
e.preventDefault();
var customer = $("input[name=customer]").val();
var details = $("input[name=details]").val();
var dataString = 'customer='+customer+'&details='+details;
$.ajax({
url: "tasks/add",
type:"POST",
beforeSend: function (xhr) {
var token = $('meta[name="csrf_token"]').attr('content');
if (token) {
return xhr.setRequestHeader('X-CSRF-TOKEN', token);
}
},
data : dataString,
success:function(data){
console.log(dataString);
alert(data);
},error:function(){
alert("error!!!!");
}
}); //end of ajax
});
});
Anybody has had this issue before?
You are using Ajax to call your Route method. So when Route::post(...) returns the view 'secrets', it returns it to the Ajax method and becomes held by the data variable. Saying return in your Routes file doesn't magically mean redirect to a certain view, it is just like any other function that returns a value.
You currently have alert(data) which just says make an alert with whatever is held by data which in this case is the html text of your view.
Instead, take out the alert() and put
window.location.replace('<path/to/secrets>')
to redirect to the page you want upon success.
Assuming your Routes file has something like :
Route::get('/secrets', function() {
return view('secrets');
});
You could say
window.location.replace('/secrets')
Have some problems to populate a Select2 with json data retrieved by ajax.
I check all samples from Select2-Github-AjaxData and other from StackOverFlow so always have same problem... the Json retrieved can't update next select2.
Some tries i use Jquery.Ajax to retrieve and assign:
function loadvariedad() {
var productIDVal= $("#frb_producto").val();
$.ajax ({
url: "http://www.fruitbull.info/api/json/es/v",
data: {idv: productIDVal, key:"123456"},
delay: 250,
dataType: 'json',
success: function(theResponse) {
$("#frb_variedad").select2({
data: theResponse.items
});
}
});
};
Other solution checked was the sample on Github form Ajax:
var productIDVal= $("#frb_producto").val();
$('#frb_variedad').select2({
ajax: {
url: 'http://www.fruitbull.info/api/json/es/v?key=123&idv='+productIDVal,
processResults: function (data) {
return {
results: data.items
};
}
}
});
Any idea or help to check?
My sample and tries on Fiddle
Solved by json origin data was formatted incorrectly
Two Ajax calls. They both execute the calls but the first one returns a drop down values in the text input field and the second one with extraParam fails to show the drop down with the matching values in text input field. I have checked, the right values are returned from the Spring Controller. Any reason why this is not working? What the difference between both the calls other than passing an extra parameter?
$(document).ready(function() {
$( ".oid" ).autocomplete({
source: "${pageContext. request. contextPath}/app/X.htm"
});
});
$(document).ready(function() {
$( "#fifa" ).autocomplete({
source: function (request, response) {
$.ajax({
url: '${pageContext. request. contextPath}/app/Y.htm',
data: {
term: request.term,
extraParam: OneMoreParamValue
},
success: function (data) {
console.log('response=', data);
}
});
}
});
});
You didn't call the response function, http://api.jqueryui.com/autocomplete/#option-source
Something like
success: function (data) {
console.log('response=', data);
response(data);
}
you might have to set the data type to 'json' as well.