How to POST json to the Wufoo Entries API? - javascript

The current documentation is a little lacking on how exactly to submit forms via Ajax. There is The Entries POST API but it talks only about xml, and doesn't even show an example payload.
I see that Wufoo has a half-built, abandoned jQuery plugin wufoo/Wufoo-jQuery-API-Wrapper which seems to do little more than wrap $.get and format errors a bit. POST is listed as a "todo".
I've tried hitting the API with things like:
{
"Field1": "first",
"Field2": "last",
"Field3": "email#example.com",
"Field4": "test messsage",
}
And based on the line "This call would contain POST parameters in name/value pairs" and the example postAuthenticated(array('Field1' => 'Frank')); I tried just sending an array of arrays.
[
['Field1', 'first'],
['Field2', 'last'],
['Field3', 'email#example.com'],
['Field4', 'test messsage']
]
But since those are obviously the wrong format, I always get the following in response.
{
"Success": 0,
"ErrorText": "Errors have been <b>highlighted</b> below.",
"FieldErrors": [
{
"ID": "Field3",
"ErrorText": "This field is required. Please enter a value."
},
{
"ID": "Field4",
"ErrorText": "This field is required. Please enter a value."
}
]
}
Does anyone have any idea how to format these requests? Maybe someone with more experience with CurlService could interpret it from their example, but I can't make heads or tails of that documentation, nor find any examples online.

I should have known. The service doesn't accept json, it only replies in json. Submitting a regular urlencoded form body works.

Related

How to get back valid JSON from mysql column where a javascript object is stored

I have some data I need to store inside a mysql table. The table has a few columns, among them a book and authors column, both of which are TEXT fields. I need to store a javascript object in the book column and an array of objects in the authors column.
Inside my angular component, I have the book object and authors array of objects.
const book = {
title: "hello world",
subtitle: "learn C programming in a week",
cover: "path/to/cover.png"
};
const authors = [{id: 3, name: "John Doe"}, {id: 4, name: "Jane Doe"}];
The object I'm passing to my service to handle the post request looks something like the following.
{
id: this.generateId(),
category: this.category,
book: JSON.stringify(book),
authors: JSON.stringify(authors),
price: this.price
}
Both of these are being stringified via JSON.stringify(...) and then successfully being posted to the database. No problem there.
I should also mention that the object values are being grabbed and put into an SQL statement in the POST request like so...
`INSERT INTO library (id, category, book, authors)
VALUES ('${obj['id']}',
'${obj['category']}',
'${obj['book']}',
'${obj['authors']}')`
However, when I try to retrieve data from that table via my angular service, angular throws the error Http failure during parsing for http://localhost:4200/.
When I fire the same request via postman, I get back the data but then when I check the validity of the returned JSON, it's not valid. The returned data looks something like this...
The title" gets highlighted with the error Expecting comma, colon or }, not string
[
{
"book": "{
"title": "hello world",
"subtitle": "learn C programming in a week",
"cover": "path/to/cover.png"
}",
"authors": "[{"id": "3", "name": "John Doe"}, {"id": "4", "name": "Jane Doe"}]"
}
]
What am I missing? How do I get back valid JSON?
"book": "{ - this looks like you have stringyfied the two values separately and then added to one object?
If so create one object first and stringify the entire thing.

How Do I See the Output of Changes to JSON

Updated to try to be more clear given the comments (Thank you for comments)
I apologize in advance for this question. I may just not have the vocabulary to properly research it. If I have an array of objects called restaurants stored in my project, for example:
restaurants: [
{
"name": "joes's pizza",
"url": "joespizza.com",
"social properties": {
"Facebook":"fb.com/pizza",
"Instagram":"instagram.com/pizza",
"googlePlus":"pizza+",
"Twitter":"twitter.com/pizza"
}
},
{
"name": "tony's subs",
"url": "tonys.com",
"social properties": {
"Facebook":"fb.com/subs",
"Instagram":"instagram.com/subs",
"googlePlus":"subs+",
"Twitter":"twitter.com/subs"
}
},
{....}
]
I then run a function to add a unique idea to all the objects in the array. The result of console.log(restaurants) is this:
{
"id": 3472,
"name": "joes's pizza",
"url": "joespizza.com",
"social properties": {
"Facebook":"fb.com/pizza",
"Instagram":"instagram.com/pizza",
"googlePlus":"pizza+",
"Twitter":"twitter.com/pizza"
}
},
{
"id": 9987,
"name": "tony's subs",
"url": "tonys.com",
"social properties": {
"Facebook":"fb.com/subs",
"Instagram":"instagram.com/subs",
"googlePlus":"subs+",
"Twitter":"twitter.com/subs"
}
},
{....}
]
I would now like to have this updated array of objects available to look at in my project, via the text editor, as a variable or restaurants.json file. How do I actually see the new modified json array and how do i save it so that i can work with it the same way i did this one above? I am currently doing this in the browser. I can see the results if i log it to the console but I need to be able to work with the new output. Thanks for taking the time to answer.
You can encode/decode JSON with JSON.stringify() and JSON.parse().
Aside from converting to/from JSON, you work with standard JS objects and arrays:
var array = JSON.parse(json_str);
array[0].address = "5th Avenue";
console.log(JSON.stringify(array));
Well, there's really not enough information in your question but I assume a few things:
You've loaded the json data from somewhere and it has been turned into a javascript object.
You've edited the object somehow and wish to convert it back to json and save the changes.
Assuming the above to be true, you just need to serialize the object back to json and submit it back to your server where you can save it in any manner you deem appropriate.
You can serialize the javascript object with JSON.stringify() (see https://stackoverflow.com/a/912247/4424504)
Add the serialized json to a hidden field on the form and submit it.
On the server when processing the form submission, grab the data from the hidden field and do with it what you wish.
Or get it back to the server any way you wish (ajax call, whatever) the key point is to serialize the object to a json string and save it
Hope that helps...

Retrieve the value from a JSONP formatted data object

Just when I think I've got he hang of identifying an element in an object, I run into a scenario that I cannot seem to get the value I want.
This part works and the data returned is correct: I have a map and when I attempt to identify a building on the map, I receive the following json object (this has been shortened for readability but in real life, its properly formatted): The function MapClick(queryResults) is called when the map is clicked.
dojo.io.script.jsonp_dojoIoScript19._jsonpCallback({
"results": [
{
"layerId": 5,
"layerName": "Building",
"value": "Name of item clicked",
"displayFieldName": "name",
"attributes": {
"ID": "123",
"name": "Name of item clicked",
"Variable1": "Some bit of information",
"Variable2": "Some other bit of information",
...
...
All I'm trying to do is return either the results[0].value OR results[0].attributes.name which in this example should bring back "Name of item clicked". The layerId, layerName, value, and displayFieldName are the "common most accessed data" so they are returned but the same information is also found within the attributes.
I've tried console.log(results[1].attributes.name); and console.log(results) with no success.
Turns out the name of the function handling the MapClicked is queryResults was needed so the correct answer is: queryResults[0].value and when you see open brackets [, you can be sure you will need [ some number ] (e.g. queryResults[0].value or queryResults[99].someothervariable. Or at least I think this is a correct statement.

How to generate a dynamic search form for Django?

I wrote a REST API based on Django's ORM. Here's some examples of my REST API
curl "http://example.com/api/pcserver?last_update__lt=2012-06-01 00:00:00&hostname__startswith=xen"
On server side, I uses a queryset.filter() function to process all the GET parameters, and convert the result queryset into a json, then return to user.
Now I'm having trouble convert this API into web based search form.
What I want to have about this magic search form:
dynamic add/delete query parameters using javascript, join them using AND (I don't need OR between these parameters)
provide drop down menu to select the field to be searched based on the model
support for selecting operators e.g. __lt, __gt, __regex
Are there any wheels out there so I don't have to re-invent it? Thank you:-)
#yegle
I'd suggest to use this media type: http://code.ge/media-types/collection-next-json/ it's an extension of the: http://amundsen.com/media-types/collection/format/ media type and you can achieve your goal without problems.
Look at this document:
{"collection": {
"version": 1.0,
"queries": [
{
"href": "http://service.com/my-resource",
"rel": "search",
"prompt": "Enter search string",
"data" : [
{
"name": "query",
"prompt": "Search query",
"required": true
}, {
"name": "gender",
"prompt": "Gender",
"list": {
"default": "female",
"options": [
{"value": "female", "prompt": "Female"},
{"value": "male", "prompt": "Male"}
]
}
}
]
}
]
}}
with "queries" array you can describe several search templates which easily can be transformed to the HTML form. In each query object you can define:
Search URI
Title of the "form"(i.e. prompt); and
Any number of query parameters with different requirements.
As you see from example above there are two query parameters - "query" and "gender", of which "query" parameter can be converted to HTML.input and "gender" parameter to HTML.select.
I hope I correctly understood your question and goal.
I am not sure about django tools.However,you can use MYSQL's full text boolean search to achieve most of the above requirements.Refer the below link and revert back with your comments!
http://dev.mysql.com/doc/refman//5.5/en/fulltext-boolean.html
Have a look at django-filter
Or, in some hacking way, use Django Admin. You could customize a ModelAdmin for the target model and use ChangeList to deal w/ querystring. Take built-in User for example:
from django.contrib.admin.options.IncorrectLookupParameters
from django.contrib.admin.views.main import ChangeList
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
self = UserAdmin(User, None) # or taken from admin.site._registry if the model admin is registered
cl = ChangeList(request, User, self.list_display,
self.list_display_links, self.list_filter, self.date_hierarchy,
self.search_fields, self.list_select_related,
self.list_per_page, self.list_max_show_all, self.list_editable,
self)
# then cl.get_query_set would process querystring and generate the queryset
try:
cl.get_query_set(request)
except IncorrectLookupParameters:
'encounter an invalid lookup'

Using jQuery's serializeArray with Rails forms

I'm trying to display an order summary on the final screen of a checkout process (buit in Rails 3). Nothing is submitted prior to this, so I'm using jQuery's serializeArray() to get all the names and values from the form fields, which works great. I collect a variety of nested attributes, so the returned array is rather busy, with names like order[donations_attributes][0][amount_in_dollars].
Is there a straightforward way, in Javascript (or using jQuery) to convert those names and values to a JSON string (which would make it way easier to work with to produce my summary output). For example, something like:
{
"order": {
"donations_attributes": [
{
"amount_in_dollars": 50.95,
"category": "Some Fund"
},
{
"amount_in_dollars": 90.92,
"category": "Some Other Fund"
}
],
"billing_address_attributes": {
"first_name": "Bob",
"last_name": "Smith",
"address1": "123 Whatever Street",
"and so on": "etc"
}
}
}
Keep in mind that I haven't submitted anything yet (nor can I), so I can't do it in Ruby. Is there a sort of obvious, straightforward way to do this, or will I need to parse out and build the string by hand?
You can pass that data as-is to the data parameter in JQuery.ajax(...), and it will end up in parama in the controller method and view for the action you call, .e.g params[:order][:donations_attributes][0][:category] == "Some Fund".
If you want to load a new url into the browser, you can use JQuery.serialize() on the data and append it to your url (with a `"?" in between) to create the URL with the appropriate query string.

Categories

Resources