ASP.NET Multiple values sent using angular $http or $resource - javascript

If I submit a form using POST, the ASP.NET server can access each value by name. However, if I do it with javascript like:
$http({
method: "POST",
url: TDSV.ROOT_PATH + "/themes/" + data.id,
params: { "xHttpMethodOverride": "PUT" },
data: { "newContent": JSON.stringify({ properties: data.json.properties, "otherInfo": "hello world" }, null, "\t") },
cache: false
});
It is all squashed together into a giant stream that I have to parse through. I have no interest in sending files this way. I just want to separate the strings by name. Is there a way to do this?

It looks like you are using WebForms, if that´s the case you must send the data in a different way.
Here is a tutorial, please take a look and let me know if that helps:
http://www.bennadel.com/blog/2615-posting-form-data-with-http-in-angularjs.htm

Related

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[]');

Javascript / jQuery / AJAX - Request string from website

I'm trying to create a function, using an API, to get the definition in french of a word.
I'm using this API:
"http://www.igrec.ca/project-files/wikparser/wikparser.php?word="
+ word +
"&query=def&count=1&lang=fr"
This url returns one definition in plain text of the word entered.
e.g: http://www.igrec.ca/project-files/wikparser/wikparser.php?word=manger&query=def&count=1&lang=fr
How does one manage to get this text? I looked at similar questions, some mention Ajax / xmlHttpRequest but I'm pretty lost.
Thanks
P.S: I don't mind using jQuery or some other technics as long as I understand what I'm doing.
The basic jQuery AJAX call can be done like this ... (using your data).
$.ajax({
type: "GET",
url: "http://www.igrec.ca/project-files/wikparser/wikparser.php",
dataType: "text",
data: {
word: word,
query: "def",
count: 1,
lang: "fr",
},
success: function(data) {
// Do something
}
});
Basically, you are performing a GET, at the URL.
The data type you expect back is text.
The data gets "transformed" into the url format from your question, then there is a success function to handle the data received.

Posting data and params using $resource using angularjs

I have a angular service which has custom methods like this
appServices.factory('RuleSets', ['$resource',
function($resource){
return $resource('', {}, {
query: {
method:'GET',
isArray:true,
url: '../data/routing-ruleset-:ruleSetId.json'
},
unschedule: {
method: 'POST',
url: '../data/unschedule:ruleSetId.json'
},
schedule: {
method: 'POST',
params: {ruleSetId: ':ruleSetId', date: ':date'},
url: '../data/schedule.json'
}
});
}]);
I am having issues posting data using my custom methods
RuleSets.unschedule({ruleSetId: ruleSetId});
and
RuleSets.schedule({date: $scope.formattedDate, ruleSetId: $scope.selectedRuleSet})
The behavior i see int he former is that the ruleSetId url parameter does not get populated if it is a POST request. In the latter, I do not know how to populate the request parameters if it is a post request (I am aware the code written is incorrect), as what I have tried in my services function does not work. I would also like to send data as part of the request to 'schedule'. I have seen how I can do this by say doing this
var ruleSets = new RuleSets();
ruleSets.id = $scope.selectedRuleSet
ruleSets.$save();
but how do I do it with a custom method?
Should I rather be using $http or not so many custom methods. I like the structure that custom methods provides, so i'd like to keep it like that if possible.

Use angularjs to post JSON without a parameter name

Currently I have a resource like so:
return $resource(usersUrl, {}, {
//The data model is loaded via a GET request to the app
query: {method: 'GET', params: {}, isArray: false},
putupdate: {method: 'PUT', params:{}}
});
Now I would like to put some JSON that looks like:
{"providerid":"userpass","firstname":"t","lastname":"b","fullname":"t b","email":"emailaddress,"avatarurl":"http:/.....","passwordset":true}
Anyway as you can see it doesn't have a top level name, if I pass this information in as a parameter to the resource a name is appended to the json like:
myparam:{"providerid":"userpass","firstname":"t","lastname":"b","fullname":"t b","email":"emailaddress,"avatarurl":"http:/.....","passwordset":true}
Is there a away of preventing this from happening as the server side doesn't like it?
Thanks
Tom
From your question it sounds like you are trying to use $resource to post some arbitrary json data. If this data is not a Resource() you should simply use $http.
$http.put(theUrl, theJsonData);
If it is actually a Resource() you can just call the method you declared when building your resource.
myResource.putupdate();

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