2

everyone

i have searched but not found the way how to pass 2d array in http post request. i want to sent 2d array in body of http reqeust.

Here is code

 let body = JSON.stringify({outlets_attribute[][name]: outlet});<--- giving syntax error here

return this.http.post('URL',body ,{headers:  this.Get_Header()});

if someone know, please guide how to handle this. Thanks!

3
  • this.http.post(url, the2dArray);? Given that what you have is syntactically incorrect, it's hard to understand what you want to achieve with that made up syntax. Commented Nov 24, 2016 at 7:34
  • i want to send {outlets_attribute[][name]: outlet} this in my http post request but don't know the proper method for doing that.... Commented Nov 24, 2016 at 11:57
  • The problem is that this is completely invalid JavaScript. So I don't understand what you actually want to do. An object has keys which are strings, and values which are anything. You're trying to use outlets_attribute[][name] as key, which is not a valid attribute name, and outlet as value. Moreover, I have no idea of what these variables are. Commented Nov 24, 2016 at 12:41

1 Answer 1

1

You can use URLSearchParams:

import { Http, Headers, RequestOptions, URLSearchParams } from '@angular/http';
import { Observable }     from 'rxjs/Observable';
import 'rxjs/add/operator/map';

with something like:

let params = new URLSearchParams();
    params.append('array[]', 'val1');
    params.append('array[]', 'val2');
return this.http.post('/endpoint', params).map(
      (response) => response
    )
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.