Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajax methods not parsing settings objext #2139

Closed
javorka opened this issue Nov 17, 2016 · 9 comments
Closed

Ajax methods not parsing settings objext #2139

javorka opened this issue Nov 17, 2016 · 9 comments

Comments

@javorka
Copy link

javorka commented Nov 17, 2016

Ajax methods is not parsing settings object, but treat it as url string. Docs here

RxJS version:
^5.0.0-rc.1
Code to reproduce:

import Rx from 'rxjs'
import { ajax } from 'rxjs/observable/dom/ajax';

function getFolderStructure(action$) {
  return action$.ofType(types.GET_FOLDER_STRUCTURE)
    .flatMap((action) => ajax.get({
        url: '/api/some/endpoint',
        body: { problem: 'here' },
        responseType: 'json'
      }).map(actions.getFolderStructureCompleted)
    );
}

Expected behavior:
Generates request
http://localhost:8080/api/some/endpoint/?problem=here
Actual behavior:
Generates request
http://localhost:8080/[object%20Object]
Additional information:
Whole settings object passed into the method ajax.get() is handled like url

@kwonoj
Copy link
Member

kwonoj commented Nov 17, 2016

Documentation you've referred is for v4, and v5 has breaking changes compare to v4 and docs are not compatible. Currently ajax interface for get is get(url: string, headers?: Object): Observable<AjaxResponse>;, does not support object as parameter. (

get(url: string, headers?: Object): Observable<AjaxResponse>;
)

@javorka
Copy link
Author

javorka commented Nov 17, 2016

@kwonoj Thank you. Can you give me a working example (without typesctipt) please?

@kwonoj
Copy link
Member

kwonoj commented Nov 17, 2016

Test cases are usually most straightforward code snippet example, for cases like get it can be found at https://github.com/ReactiveX/rxjs/blob/master/spec/observables/dom/ajax-spec.ts#L415

@javorka
Copy link
Author

javorka commented Nov 18, 2016

@kwonoj Now, it is correctly parsed, but with GET request, body param is ignored. POST and PUT requests are working. Is there a way to add url parameter in GET similar to jQuery param() ?

@kwonoj
Copy link
Member

kwonoj commented Nov 18, 2016

@javorka
Copy link
Author

javorka commented Dec 1, 2016

There are several problems within ajax:

  • 1. GET request parameter parsing - the only way to create GET request with parameters is only manually creating url (ajax.get('myulr/?param=myparam'), nothing like ajax.get(url, {param: 'myparam'}) exist
  • 2. POST and PUT request nested parameter parsing - when creating request like:
const payload = {a: 1, b:2, c: {d:3}}
ajax.post(url, payload) // generates {a:1, b:2, c: "[object Object]"} 

nested parameter are not parsed

@benlesh
Copy link
Member

benlesh commented Dec 1, 2016

Side note: You're not supposed to send a body with GET.

@stiofand
Copy link

stiofand commented Dec 4, 2017

The lack of clear examples for the latest version api is making it problematic to say the least. Tests and outdated documentation mean that its always a struggle to find any solutions. @javorka is correct, there is a range of issues with ajax, yet there is no solution or example offered. It has got to the stage where we are considering dropping the use of Observable ajax in favour of a more well documented library.

The test example snippets do not show how to pass along parameters, which I believe to be the crux of the issue here.

@F4b1n0u
Copy link

F4b1n0u commented Dec 5, 2017

I admit the doc is not great but if you are setting up the header and the body properly
you will not end up with [Object object] as parse body

here is something who works like a charm for me :)

...
headers: {
  'Content-Type': 'application/json',
},
body: JSON.stringify(people)
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants