0

I am trying to send to and from objects through string via xhr API call. These objects are defined as follows: (I've even wrapped properties in double quotes but in vain)

const to = {
        name: toName,
        address_line1: toAddres_line1,
        address_city: toCity,
        address_state: toState,
        address_zip: toZip
    };
    const from = {
        name : fromName,
        address_line1 : fromAddres_line1,
        address_city : fromCity,
        address_state : fromState,
        address_zip : fromZip
    };
var data = "to={" + to + "}&from={" + from + "}&color=false";  

tried removing spaces between " + to + " to "+to+" but no luck

While sending the property values inside data variable it is not getting passed properly. Upon console.log(to); and console.log(from); it gives me the right declaration of the objects in console which is as to={[object Object]}

Following error is thrown upon sending data to server by ```:

{
    "error": {
        "message": "from address not found",
        "status_code": 404,
        "code": "not_found"
    }
}

I tried searching if I'm passing the objects in a wrong way but couldn't hit a luck now. So, I am unable to find what is exactly wrong in my code.

  • "it gives me the right declaration of the objects in console which is as to={[object Object]}" — In what way is that "right"? All the data in the objects has been discarded in favour of a string which says "some generic object". – Quentin Jan 31 '22 at 09:49
  • We can't tell you how to pass the objects through the URL string because we don't know what format the server expects them to be presented in. – Quentin Jan 31 '22 at 09:50
  • I see the question has been closed as a duplicate. It's a slightly dubious duplicate because (a) we don't know if the server wants the data expressed as JSON and (b) it omits encoding the strings of JSON in a URL safe format. – Quentin Jan 31 '22 at 09:51
  • 1
    @Quentin ¯\\_(ツ)_/¯ I'm working with what OP provided - some objects need to be converted as string. If that's not what OP wants, they are free to clarify. It's not really our purpose to laboriously extract all the requirements from OP. We can work off the information provided. If OP makes us to guess, they get a guess. – VLAZ Jan 31 '22 at 09:54
  • @VLAZ +1 for : "If OP makes us to guess, they get a guess" good point of vue :) – SeleM Jan 31 '22 at 10:06

0 Answers0