Post a variable to json format in Ember.js - javascript

I apologize if this question seems basic as I am new to both stackoverflow and javascript in general.
My goal here is to store the user's dropdown menu selection as well as some other user inputs into variables, then I want to post the json file that contains the variables into a specific route where my serve-side javascript can read from. I have the variable part and user selection part covered, however, I am having trouble posting the variables to the specified route.
So in my app.js file in my ember framework, I tried the below code to test out if I can post {12345} to address:port/api/iwantmyjsonhere/ but when I run it and go to that specific page (address:port/api/iwantmyjsonhere/), it says cannot GET.
$(document).ready(function() {
$.ajax({ url : "/api/iwantmyjsonhere/",
type: 'POST',
dataType : "json",
data: "12345"
});
});
I understand that this question is lower level, so if you guys are too busy to answer, point me to any resources that might help me would be greatly appreciated too! Thanks in advance!

I guess this is not related to ember anywhere.
You are just using jQuery for ajax call and as per jQuery standards "Data" Object must be Key/Value pairs.
Means you should do something like -
$(document).ready(function() {
$.ajax({ url : "/api/iwantmyjsonhere/",
type: 'POST',
dataType : "json",
data: {id:"12345"}
});
});
You can read more on this one at -
http://api.jquery.com/jquery.ajax/
And if you want to implement it in ember way the you can use something like -
return Ember.$.ajax({
url: '/api',
method: 'POST',
dataType: 'json',
data: {//key value pairs}
}).then(function(result){
//After ajax is successful.
}

Related

Find definition of the 'action' inside 'data' object in jquery

I'm a back-end dev that inherited code that looks like this
xhr = jQuery.ajax({
type: 'POST',
url: ajax_url, // ERROR!!
dataType: "json",
data: {
action: 'properties', <---------------------------------
data: url_vars,
url: document.URL
},
I know that the action called properties has to be defined somewhere, but I'm not sure how to find it.
I read multiple threads on SO, but this is not a form action, so I'm not sure how one would go about finding it. If an experienced front-end dev saw this name, what would they look for specifically inside the code?
Apologies for the newbie question, but this is a bit confusing.
Closest answer I found is the data.action is an attribute action of the object data is a value that you known cos we haven't any reference, unfortunately I'm not even sure where to look for any reference apart from analyzing 1000+ times the word 'properties' is mentioned in the framework's code.
Any help is greatly appreciated.

js-routes adding (::format) to urls

I have a simple rails project that I've been playing around in with reactjs. To add some basic navigation, I brought the js-routes library in and it works great for urls that have a path parameter such as "localhost:3000/addresses/1".
The problem I am facing is I am trying to call a "new" resource method and it adds the (::format) literally to the url which of course bombs as localhost:3000/addresses/new(.:format) is an invalid path.
I reference the "new_address_path" path as specified in the routes-js docs. The rake output for this url is below:
new_address_path GET /addresses/new(.:format) addresses#new
The HTML snippet utilizing the above path looks like this:
<a href={Routes.new_address_path}>Create am address</a>
ENV:
-Ruby: 2.2.4
-Rails: 4.2.6
-js-routes: 1.2.8
Route in question:
resources :addresses
What am I missing here? It seems like it is not interpreting the rails route file properly.
I am not sure if I got your question. If you want to generate url with format suffix you can use format option in the helper method. For example:
Routes.new_address_path(format: 'js')
will generate something like this:
/addresses/new.js
Sorry I thought I posted my solution in here.
The problem wasn't js-routes but rather my AJAX call, I was setting the content type to JSON and I assumed that it was converting my object to JSON using the built in methods. This is not true, you need to manually convert the object to JSON via JSON.stringify(obj).
Old ajax call:
....
url: Routes.feedback_path(),
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: obj,
....
New ajax call:
....
url: Routes.feedback_path(),
dataType: 'json',
contentType: 'application/json',
type: 'POST',
data: JSON.stringify(obj),
....
You might have found a solution, but your original problem is one with js-routes, or rather your use of it.
You have to provide the parens to get the correct output from js-routes.
Original: Routes.new_address_path
Fixed: Routes.new_address_path()
As smefju posted, you can specify the format in the parens, but completely leaving them off is not an option.

Passing an array in JQuery with a POST and reading it with Flask

I try to store the data from a graph with jQuery but I always get a 400 Bad request.
The problem is the data_series variable isnt just an array of integers but much more. This is unchangeable since it is necessary for my chart generation to be like this.
A litle piece of it to show you what I mean:
data_series[0][data][0][]:1389975624000
data_series[0][data][0][]:91
data_series[0][data][1][]:1390003200000
data_series[0][data][1][]:446
data_series[0][data][2][]:1390089600000
data_series[0][data][2][]:429
.....
My Jquery post looks like this,
$.ajax({
url: "{{ url_for('save_graph_to_session') }}",
method: "POST",
data: {
data_series: data_series
},
success: function(data) {
console.log('Saved to session')
}
});
On flask side I read it like this, and put in a session:
#app.route('/save_graph_to_session', methods=[ 'POST'])
def save_graph_to_session():
session['data_series'] = request.form['data_series'];
return "saved"
I've tried to post with 'data_series[]:' data_series, didn't work out either.
EDIT:
Maybe the solution lies within the way to request, so :
Is there a way to request in flask that ignores the fact that this is an array of arrays
this is the way one can do this:
session['data_series'] = request.form.getlist('data_series[]');

As create json file with data in JavaScript, and send that file for JSONP other domain

I need your help with a big doubt.
Question: As I can create JSON file (with data) from JS code?
The reason is which I need use JSONP, and I wanna take that data from other domain (i.e. http://www.example.com/data/clients.json?callback=?), but this data are storage in a DB (WebSQL) and I need which that data is written in the json file (i.e. clients.json)
Please I need your help because three days ago I try do this, but I don't find yet the answer.
P.S.: Sorry for my english, not is my native language.
EDIT
Sorry my question is confused. I try again but for steps.
I need to write a JSON file with data or records from a DB (WebSQL). Actually my functions are working well (add record, update record, delete), but I need to put that data in a json file because, I'll use the next code for get the data from other domain.
(function($) {
var url = 'http://www.example.com/scripts/clients.json?callback=?';
$.ajax({
type: 'GET',
url: url,
async: false,
jsonpCallback: 'jsonCallback',
contentType: "application/json",
dataType: 'jsonp',
success: function(json) {
console.dir(json.sites);
},
error: function(e) {
console.log(e.message);
}
});
})(jQuery);

GAE on python + REST, inserting/updating db.ListProperty(users.User)

I have a simple db.Model, that has one of the fields db.ListProperty(users.User)
For the REST server I used http://code.google.com/p/appengine-rest-server/
However, I can't seem to update this field..
The app is currently password-protected but if anyone wants to take a look, I can make it public.
Basically, I have a form that I post using this jQuery:
$.ajax({
contentType: 'application/json',
url: '/rest/' + $this.attr('name') + update,
type: 'POST',
data: $this.wsString(),
});
where $this.wsString() is applying serializeArray() to the form and after that transforms the result into proper REST format ( + JSON.stringify at the end ).
Here's the metadata for the entity, the "developers" field is the problematic one: http://toxik.appspot.com/Project.xml
Thanks for any help!
I managed to fix it: db.ListProperty(users.User) wants an object that serialized looks like this:
"developers":{"item":["some1#email.com","some2#email.com"]}

Categories