Reputation: 882
I am trying to take a value from the input field and use it in the url of my ajax request. First, I want to fix to input to make it all lowercase so it matches the path of the file I'm requesting. For each of the two companies, I tried a different way of doing it, but neither seemed to work. Any ideas?
function requestCompany1 (evt) {
var company1 = $('#companyInput1').val(
function(i, val) {
return val.toLowerCase();
}).val();
console.log(company1);
var url1 = 'assets/js/' + company1 + '.json'
$.ajax({
url : url1,
dataType: 'json',
data: company1,
success: function(data) {
//code the run on success
}
function requestCompany2 (evt) {
var company2 = $('#companyInput2').val().toLowerCase();
var url2 = 'assets/js/' + company2 + '.json'
console.log(company2);
$.ajax({
url : 'assets/js/spling.json',
dataType: 'json',
data: company2,
success: function(data) {
//code to be called on success
});
}
I know I could combine the two functions to make it easier but I just wanted to lay it out first. Thanks
EDITED added.val()
to the end of company1
Upvotes: 0
Views: 219
Reputation: 171690
First version should be logging the jQuery object for the input element to console since you have provided a function as argument to val()
method... it is now a setter not a getter.
If you changed it to following it should return the value:
var company1 = $('#companyInput1').val(
function(i, val) {
return val.toLowerCase();
}).val();
Second version you aren't passing the url variable you created to ajax options object and there is a syntax error... missing closing brace for success
function requestCompany2 (evt) {
var company2 = $('#companyInput2').val().toLowerCase();
var url2 = 'assets/js/' + company2 + '.json'
console.log(company2);
$.ajax({
url : url2,
dataType: 'json',
data: company2,
success: function(data) {
//code to be called on success
}
});
}
Sending data
to a json file doesn't make much sense unless it is a dynamic file on server. If it is you would need to provide a key/value pair to pass as data
Upvotes: 1