By TDG


2019-02-11 09:28:56 8 Comments

Requested URL sent - https://www.example.com/detail.guest.html?ppc=FDE466920006DCEFA697BF982FC9C87C5B257ECB2230CBF4D6D6CA740C7B894D5795F70DED928ED3B00C1F3F77DF974DFD73882DEBDD7EC063B37DEB24CF655528FD911109C57961AE314C612772AADFD2E193D572E6F6C8E249A6DAA

Get below response data correctly as expected by 3rd party.

BookersID=250100000002;BookersTitle=Mr;BookersFirstName=test1;BookersLastName=test2

enter image description here


I want to extract "BookersID", "BookersTitle", "BookersFirstName", "BookersLastName" separately and display this value in input field.

JS:

var bookerID = data[0].BookersID;
var bookerTitle = data[0].BookersTitle;
var bookerFname = data[0].BookersFirstName;
var bookerLname = data[0].BookersLastName;
console.log("BookersID", bookerID);
console.log("BookersTitle", bookerTitle);

But getting error in display value.

Please let me know how to get the value in console log?

Thanks

4 comments

@Nick Parsons 2019-02-11 09:54:15

You could use .reduce() and .split() to create your string into an object, which can then have its properties accessed

const data = "BookersID=250100000002;BookersTitle=Mr;BookersFirstName=test1;BookersLastName=test2";

const dataObj = data.split(';').reduce((acc, kvp) =>
  ({
    ...acc,
    ...(([key, value]) => ({[key]: value}))(kvp.split('='))
  }), {});

console.log(dataObj);
// access properties:
console.log(dataObj.BookersID);

@Patryk Uszyński 2019-02-11 09:35:32

First you need to get data from your xhr request. To do that you need to add callback function. (More info in jQuery.get() documentation)

$.get( endpoint, function( data ) { // add callback to handle response
 // ... parse data here
});

As I understand you need to parse data. It could be done by using String.prototype.split method and simple mapping.

console.log(data) // BookersID=250100000002;BookersTitle=Mr;BookersFirstName=test1;BookersLastName=test2
var parsed = data.split(';').map(part => ({ name: part.split('=')[0], value: part.split('=')[1] }));

console.log(parsed);

Output:

[
   {name: "BookersID", value: "250100000002"},
   {name: "BookersTitle", value: "Mr"},
   {name: "BookersFirstName", value: "test1"},
   {name: "BookersLastName", value: "test2"}
]

If you want to get data as an object:

var parsedObject = parsed.reduce(
    (obj, item) => Object.assign(obj, {[item.name]: item.value}) ,{});
// {BookersID: "250100000002", BookersTitle: "Mr", BookersFirstName: "test1", BookersLastName: "test2"}

@TDG 2019-02-11 09:43:35

Hi.. Pls check attached image. I will get response like this. Not sure whether we need to parse or directly get the string value. I need to get value from here (attached screenshot) in my comments. thanks

@Patryk Uszyński 2019-02-11 09:44:25

@TDG what is inside of your data variable?

@TDG 2019-02-11 09:47:00

Not sure.. on submit that encrypted url.. i will get this value as response. But not sure how to split and display in input field. I am too confused with "data" variable.

@Patryk Uszyński 2019-02-11 09:48:01

@TDG how do you submit it?

@TDG 2019-02-11 09:51:02

makeAjaxRequest( endpoint ) { if (typeof endpoint == 'undefined' || endpoint == '') return; return $.get(endpoint); }

@TDG 2019-02-11 09:51:55

Sure.. your code will work fine. But how to bring response strings into data variable? I struck in this area only.

@Patryk Uszyński 2019-02-11 09:59:16

@TDG check this now

@TDG 2019-02-11 11:46:49

@partyk.. thanks for your reply. All ok.. but, i want to insert BookersID value into input field. $('.bookersIDInput').val(BookersID.value) will it work?

@Patryk Uszyński 2019-02-11 12:07:53

@TDG $('.bookersIDInput').val(parsedObject.BookersID) should work ;)

@TDG 2019-02-11 12:14:43

Thanks.. it works

@mukund 2019-02-11 09:48:26

var str = 'BookersID=250100000002;BookersTitle=Mr;BookersFirstName=test1;BookersLastName=test2';
var data = {};
var parsed = str.split(';').map(part => { let x = part.split("="); data[x[0]] = x[1]; console.log(x) });
console.log(data)

Output:

{BookersID: "250100000002", BookersTitle: "Mr", BookersFirstName: "test1", BookersLastName: "test2"}

@Ashok Vishwakarma 2019-02-11 09:38:28

If you getting the same response you need to write a utility function to convert the same into an object

function _convert(responseString) {
  var _obj = {};

  responseString.split(";").forEach(function(pair){
     var _pairArr = pair.split("=");
     _obj[_pairArr[0]] = _pairArr[1];
  });

  reuturn _obj;
}

var responseString = "BookersID=250100000002;BookersTitle=Mr;BookersFirstName=test1;BookersLastName=test2";
var obj = _convert(responseString);

obj['BookersID']; // 250100000002
// or
obj.BookersID; // 250100000002

Note: This will only work if your response has exactly the same format as you have mentioned.

@TDG 2019-02-11 09:43:52

Hi.. Pls check attached image. I will get response like this. Not sure whether we need to parse or directly get the string value. I need to get value from here (attached screenshot) in my comments. thanks

Related Questions

Sponsored Content

56 Answered Questions

[SOLVED] How to get the value from the GET parameters?

  • 2009-06-11 08:32:49
  • joe
  • 1869197 View
  • 1175 Score
  • 56 Answer
  • Tags:   javascript url

73 Answered Questions

[SOLVED] How can I get query string values in JavaScript?

35 Answered Questions

[SOLVED] How do I return the response from an asynchronous call?

22 Answered Questions

[SOLVED] Get selected value in dropdown list using JavaScript

20 Answered Questions

[SOLVED] Get the current URL with JavaScript?

  • 2009-06-23 19:26:45
  • dougoftheabaci
  • 2645408 View
  • 2805 Score
  • 20 Answer
  • Tags:   javascript url

28 Answered Questions

[SOLVED] jQuery Get Selected Option From Dropdown

  • 2012-05-18 20:11:54
  • William Kinaan
  • 1316175 View
  • 1040 Score
  • 28 Answer
  • Tags:   javascript jquery html

14 Answered Questions

30 Answered Questions

[SOLVED] Get current URL with jQuery?

31 Answered Questions

Sponsored Content