A
A
Aleksandr2018-07-20 12:36:01
Vue.js
Aleksandr, 2018-07-20 12:36:01

How to explain to a back-end developer why I can't do what he wants?

I have already asked the question. But I repeat once again
There is such a piece of code

'formData.brand'(val) {
    let payload = {
        names: arr,
        values: {
            "brand": val
        }
    }
    if (!val) return;
        axios.get('../../../json/v1/form/kasko/dependencies', {
            params: payload
        }).then(response => {
            this.$set(this.dependentData, 'brand', {
                model: response.data.models,
                country: response.data.city
            });
        }).catch(errors => {
            console.error(errors);
        });
}

in valuesI transfer object
On back the line comes; Becker demands that he receive an object
5b51acdd1e1ee358756848.png
Sent such a screen to me
5b51aceaa1d2e624263030.png
How can I explain to him normally that I cannot do what he wants?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir, 2018-07-20
@Sashjkeee

Learn query methods. With GET you send a string, to send an object you need to use one of POST, PUT, PATCH and the send object parameter
https://developer.mozilla.org/uk/docs/Web/HTTP/Methods
https://learn.javascript .ru/ajax-xmlhttprequest
https://developer.mozilla.org/en-US/docs/Web/API/F...
Your piece of code

axios.get('../../../json/v1/form/kasko/dependencies', {
            params: payload
        }).then(response => {

A piece of code from the documentation
Performing a POST request
axios.post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

Teach the backend to write an API contract in https://swagger.io/ then there will be less misunderstanding

S
Sergey delphinpro, 2018-07-20
@delphinpro

You can do whatever the backender wants.
The problem is with Axios. GET parameters, if they contain multidimensional arrays/objects, it converts to json representation, leaving only the top-level linear array. In general, correctly (this was already noted by Vladimir ), GET is for requesting data, and the request parameters usually come in a list.
But still, the protocol allows you to represent arrays, both linear and multidimensional. jQuery does just that, by the way.
In general, to the point. You can use standard Axios features to transform query parameters:

let arr = [1,2,3];
let val = 'BMW';
let payload = {
    names : arr,
    values: {
        brand: val,
    },
};

axios.get('http://site.ru?id=1', {
    params: payload,
    paramsSerializer: function(params) {
        let tmp = [];
        params.names.forEach(item => {tmp.push(`names[]=${item}`)});
        for (let name in params.values) {
            if (!params.values.hasOwnProperty(name)) continue;
            tmp.push(`values[${name}]=${params.values[name]}`);
        }
        return tmp.join('&');
    },
}).then( ...

As a result of the request, the programmer will receive data in the &_GET array in the following format:
Array
(
    [id] => 1
    [names] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )
    [values] => Array
        (
            [brand] => BMW
        )
)

The serializer will have to be written according to the data format, or try to figure out something universal.

J
Jhn Doe from, 2018-07-20
@spbislanders

wait, what are you doing? you send to the backend:
- an array of strings where names is a query param and a GET request type, that's ok
- in query param: values ​​which is an object {"brand": "bmw"}, what's that?
you can of course do it like this:

const url = '../../../json/v1/form/kasko/dependencies?values=' 
const values = {
  "brand": val
}

axios.get(url + JSON.stringify(values))
  .then(response => {....})
  .catch(...);

how to form the string ?names[]=models&names[]=oiltype in a loop, I think you can guess for yourself, it will also need to be added to the url variable.
if the data to create a new row in some table goes away, then I would better send axios to the backend post request with data and content-type: application/json
something like:
data = {
   "names": [{
     "models": [{"brand" "bmw"}]
   }]
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question