Create Local Javascript Array From jQuery.get() AJAX Call - jquery-plugins

I'm using the jQuery Autocomplete plugin and would like to store all the autocomplete values locally in an array. I'm able to retrieve a comma separated list from a url using jQuery.load() or jQuery.get(). However, I can't load the data into an array successfully.
Any suggestions on how to get this working? I realize there's probably a much better way. Any help would be appreciated.
Thanks!

Are you sure that it isn't working properly, but that you're not trying to access that variable outside of the async ajax call?
The only way to have immediate access outside of that ajax call is to run it async=false, or to use a timer to wait and check for that value.
If this is the case, look to the docs on the ajax abstractions:
http://api.jquery.com/jQuery.ajax/
Specifically:
var html = $.ajax({
url: "some.php",
async: false
}).responseText;

When you say "comma separated list" I assume you mean a string (like "foo1, foo2, foo3")?
If so, you can use the split function of the string type like so:
var data = "foo1, foo2, foo3";
data.split(",");
// ["foo1", "foo2", "foo3"]

Related

Ajax load data to several places at once

I would like to load a page and then fill in some placeholders on it with data via Ajax request. I did that with jquery, but the problem is this: the file that I fetch with Ajax is time-consuming, and I was able to load data only to one placeholder at a time: $("#div1").load("info.php");.
I would like to send data for all placeholders in one response but can't get the idea how can I put it into all placeholders at once.
Please advice if this is possible.
I'd really like to load array of values via Ajax then put it into placeholders (divs) with some for in JS, but can't find a way to.
If you're trying to get the data from one request then try one ajax call and iterate like so...
array = {'id': {'pointer': 'div_id', 'content': 'the content'}}
//in a for loop -> item
$(item['pointer']).html(item['content'])

reading data from Ruby in D3 (JSON)

i'm confused how to read data from ruby class.
assume i have a class, called "Points".
what i want is reading the all data from this 'Points'.
what i did is like this :
var allPoints = <%= Point.all.to_json %>;
d3.json('allPoints',
function(data){ .......}
i dont know why but somehow d3 can't read the varable allPoints.
is there maybe something i forget to put in code?
or maybe there's another way to read all data from Ruby?
d3.json() is used for performing an AJAX call and retreiving JSON data from a server, which is then passed to the provided callback. It appears that what you're doing is generating a page with your data embedded in the page as a JavaScript. To use this object, just use it. Use whatever code was in your callback function, replacing the name data with allPoints.

Send ONE Javascript variable to PHP via AJAX

I've been looking around the forums without a solution to my problem. It's pretty simple, really, and I'd appreciate it if you could explain your answer if you would be so kind.
I'm new to AJAX and Javascript, and I need to send one variable from my javascript code and basically "convert" it into php. Here's what I have so far:
var selected = rowData.ID
jQuery.ajax({
url: "test.php",
type: 'POST',
data: { selected },
cache: false
});
I use this selected value further down in the code. I use PHP to display the (value of selected).
"vars": [
"(value of selected)"
],
However, I can't seem to make my ajax request work and send the variable to my PHP file. Here's what my PHP file looks like:
$row = $_POST["selected"];
Thanks in advance for the help.
try replacing your "data:" with this:
data: { 'selected': selected },
So this is very delayed answer, but I was having trouble getting a variable to send too. I'm not using php, but saw loads of examples like vlscanner gave, but who knows why it didn't work.
I stumbled across this explanation of how to send multiple parameters, and it works just as lovely for sending one parameter.
http://weblog.west-wind.com/posts/2012/May/08/Passing-multiple-POST-parameters-to-Web-API-Controller-Methods
multiple:
data: JSON.stringify({ Album: album, User: user, UserToken: userToken }),
or just one:
data: JSON.stringify({ Album: album}),
I'm no expert on timing,efficiency and all that, and it's possible that JSON.stringify adds unnecessary bulk and there is possibly some valid reason that sending data without the JSON.stringify didn't work. However, if you are in a bind and need something to work, this might help those of us still asking this question.
I'm suspecting that mine did not work because I was sending it to an asp method that likely requires the parameters to come as a JSON string. I have to research that next. Every step is a new discovery.

jQuery AJAX Form, cannot send array?

I'm practicing my jQuery skills (ok, learning too) and experienced an issue. I got a file upload form with file input. I'm using this plugin (http://www.fyneworks.com/jquery/multiple-file-upload/#tab-Uploading) to upload multiple files at once. So I'm using following form input:
<input type="file" class="multi" name="photo[]" accept="gif|jpg|jpeg|png" maxlength="5"/>
Now... I'm trying to send an AJAX request to php file that will handle upload and server-side validation:
$('#upload_photos_s').click(function(q){
var photo = $('[name=photo[]]').val();
// Process form
$.ajax({
type: "POST",
url: "upload.php",
data: 'photo[]='+photo,
success: function(html){
alert($('[name=photo[]]').val());
$("#photo_upload_form").html(html);
}
});
return false;
});
While using Firebug I can see that there's only one file in photo[].
Any suggestions why? Is there something I missed?
Regards,
Tom
As it stands, you are indeed querying the value of the first photo[] member only. photo[].val() will not return an array containing all the values.
You would have to run through each member of photo[], e.g. using each(), to build an array of values.
However, I'm not sure this is the right path to go for whatever you want to do. You are aware that what you are doing is uploading the file names only, not their data?
It is not possible to upload files using AJAX without the help of additional tools like Flash-based SWFUPload. This is for security purposes to prevent scripts from having direct access to local files.
Maybe what you're trying to do is best suited for an approach where the form's target property points at an <iframe>. That would not trigger a reload of the page, but still submit the form the "traditional" way, allowing for old-school file uploads.
Well on the link you provided, there is a part called Ajax specifying the simplest way is to use the jQuery Form Plugin.
Documentation of plugins help a lot usually ^^.
Have a nice day :)

jquery ajax data string. How do i make an object or escape the string?

I have a button which calls jquery.ajax and submits POST data. I need to get user text from a textarea. So far, no problem. However now i need to set the post data. I have a string in the form as k=v&k2=v2 etc and then i have this user text. I obviously cant write + "&usertext=" + usertext since the text may have code and &kxxx=val which should be inside of the usertext value.
How do i set the ajax data?
http://api.jquery.com/serialize/
You can call the jQuery method serialize on a form to get a valid data structure to pass in to jQuery.ajax instead of building it from scratch:
$.ajax({
data: $('#myform').serialize()
});
For the values outside of your textarea you can then add them as hidden inputs in your form and they'll be pulled in to the resulting serialize data.
there are at least 2 options:
1. use http://jquery.malsup.com/form/ jQuery plugin, see 'ajaxSubmit' method
2. use 'serialize' method of jQuery (seee here for details)

Resources