ITsolutionWizard
asked on
jquery, each statement
I just need to know how to get the list of shiptoName. I know I can use each statement but no idea how to deal with "\".
Please helps.
Thanks
RETURN response from wcf
{
"PackageKey": null,
"PackageNo": 1,
"TotalPackages": 1,
"Status": "Ok",
"Data": "{\"Shiptos\":[{\"ShiptoNo \":\"00\", \"ShiptoNa me\":\"DAT A SALES CO. INC.\",\"ContactName\":\"\ ",\"Contac tTitle\":\ "\",\"Addr ess_1\":\" JOHN SMITHS\",\"Address_2\":\"3 450 WEST\",\"Address_3\":\"\", \"City\":\ "BURN\",\" State\":\" MN\",\"Zip \":\"52337 \",\"Phone \":\"888-8 88-8838\", \"CellPhon e\":\"\",\ "Fax\":\"\ ",\"Email\ ":\"\"}],\ "DealerNo\ ":\"305033 \",\"Deale rName\":\" DATA. INC.\",\"ContactTitle\":\" \",\"Conta ctName\":\ "\",\"Addr ess_1\":\" JOHN SMITHS\",\"Address_2\":\"3 450 WEST PARKWAY\",\"Address_3\":\" \",\"City\ ":\"BURNSV ILLE\",\"S tate\":\"M N\",\"Zip\ ":\"55337\ ",\"Phone\ ":\"888-89 0-8838\",\ "CellPhone \":\"\",\" Fax\":\"\" ,\"Email\" :\"\"}"
}
how it is consumed
<script type="text/javascript">
function GetDealerShiptoList() {
var GetDealerShiptoListJSon = "http://clientaccesstest.abc.com/wcf/orders/RestService/QuoteService/GetDealerShiptoListJSon(066,305082)";
$(document).ready(function () {
$.ajax({
crossDomain: true,
cache: false,
type: "GET",
async: false,
dataType: "json",
contentType: 'text/plain',
url: GetDealerShiptoListJSon,
success: function (data) {
},
error: function (xhr, status, error) {
alert(xhr.responseText + " : error SelectDealerShipTo");
}
});
});
}
</script>
Please helps.
Thanks
RETURN response from wcf
{
"PackageKey": null,
"PackageNo": 1,
"TotalPackages": 1,
"Status": "Ok",
"Data": "{\"Shiptos\":[{\"ShiptoNo
}
how it is consumed
<script type="text/javascript">
function GetDealerShiptoList() {
var GetDealerShiptoListJSon = "http://clientaccesstest.abc.com/wcf/orders/RestService/QuoteService/GetDealerShiptoListJSon(066,305082)";
$(document).ready(function
$.ajax({
crossDomain: true,
cache: false,
type: "GET",
async: false,
dataType: "json",
contentType: 'text/plain',
url: GetDealerShiptoListJSon,
success: function (data) {
},
error: function (xhr, status, error) {
alert(xhr.responseText + " : error SelectDealerShipTo");
}
});
});
}
</script>
ASKER
Does it return on or more rows?
Adjustment to @Tom's suggestion:
...
success: function (data) {
var dataParsed = data.replace('\\"', "'");
dataParsed = JSON.parse(dataParsed);
// because "Data" property of main JSON object has value wrapped in double quote: "Data": "{...}"
// so "Data" property is considered as a string type instead of nested JSON object.
// we need to parse the value of "Data" property to JSON object before we can access "Shiptos"
var shiptosParsed = JSON.parse(dataParsed.Data);
var ShipToName = shiptosParsed.Shiptos[0].ShiptoName;
},
...
ASKER
It will.be.a.list so.expect.a.drop.down
After parsing inner "Data" property of main JSON object, "Shiptos" property is an array of JSON object. So you can just loop through all elements in that array and collect "ShiptoName" collection, and then fill them in your dropdown.
You may want to check with the creator of your service. It seems odd that they would be encoding the value of the "Data" property as a string when it appears to be JSON. I'm wondering if something got serialized incorrectly by the service.
ASKER
It does not work at all.
ASKER
the service format is actually below
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{
"PackageKey": null,
"PackageNo": 1,
"TotalPackages": 1,
"Status": "Ok",
"Data": "{\"Shiptos\":[{\"ShiptoNo \":\"00\", \"ShiptoNa me\":\"DAT A SALES CO. INC.\",\"ContactName\":\"\ ",\"Contac tTitle\":\ "\",\"Addr ess_1\":\" JOHN SMITHS\",\"Address_2\":\"3 450 WEST\",\"Address_3\":\"\", \"City\":\ "BURN\",\" State\":\" MN\",\"Zip \":\"52337 \",\"Phone \":\"888-8 88-8838\", \"CellPhon e\":\"\",\ "Fax\":\"\ ",\"Email\ ":\"\"}],\ "DealerNo\ ":\"305033 \",\"Deale rName\":\" DATA. INC.\",\"ContactTitle\":\" \",\"Conta ctName\":\ "\",\"Addr ess_1\":\" JOHN SMITHS\",\"Address_2\":\"3 450 WEST PARKWAY\",\"Address_3\":\" \",\"City\ ":\"BURNSV ILLE\",\"S tate\":\"M N\",\"Zip\ ":\"55337\ ",\"Phone\ ":\"888-89 0-8838\",\ "CellPhone \":\"\",\" Fax\":\"\" ,\"Email\" :\"\"}"
}
</string>
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">
{
"PackageKey": null,
"PackageNo": 1,
"TotalPackages": 1,
"Status": "Ok",
"Data": "{\"Shiptos\":[{\"ShiptoNo
}
</string>
ASKER
I tried below using your codes. still not working.
<script type="text/javascript">
function GetDealerShiptoList() {
var GetDealerShiptoListJSon = ""http://clientaccesstest.abc.com/wcf/orders/RestService/QuoteService/GetDealerShiptoListJSon(066,305082)";
$(document).ready(function () {
$.ajax({
crossDomain: true,
cache: false,
type: "GET",
async: false,
dataType: "text",
contentType: 'text/plain',
url: GetDealerShiptoListJSon,
success: function (data) {
data = data.replace('<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">', '');
data = data.replace('</string>', '');
data = data.replace('\\"', "'");
var finaldata = data;
dataParsed = JSON.parse(finaldata);
var shiptosParsed = JSON.parse(dataParsed.Data );
var ShipToName = shiptosParsed.Shiptos[0].S hiptoName;
alert(ShipToName);
},
error: function (xhr, status, error) {
// alert(xhr.responseText + " : error SelectDealerShipTo");
}
});
});
}
</script>
<script type="text/javascript">
function GetDealerShiptoList() {
var GetDealerShiptoListJSon = ""http://clientaccesstest.abc.com/wcf/orders/RestService/QuoteService/GetDealerShiptoListJSon(066,305082)";
$(document).ready(function
$.ajax({
crossDomain: true,
cache: false,
type: "GET",
async: false,
dataType: "text",
contentType: 'text/plain',
url: GetDealerShiptoListJSon,
success: function (data) {
data = data.replace('<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">', '');
data = data.replace('</string>', '');
data = data.replace('\\"', "'");
var finaldata = data;
dataParsed = JSON.parse(finaldata);
var shiptosParsed = JSON.parse(dataParsed.Data
var ShipToName = shiptosParsed.Shiptos[0].S
alert(ShipToName);
},
error: function (xhr, status, error) {
// alert(xhr.responseText + " : error SelectDealerShipTo");
}
});
});
}
</script>
ASKER
I finally codes it below. and need some helps. I am able to get the value for the city
BURNSVILLE
but it supposes to return BURNSVILLE 2 times. Can you show me how to loop after the following statement?
var shiptosParsed = JSON.parse(dataParsed.Data );
var ShipToName = shiptosParsed.Shiptos[0].C ity;
Thanks,
<script type="text/javascript">
function GetDealerShiptoList() {
var GetDealerShiptoListJSon = "http://clientaccesstest.kenwoodusa.com/wcf/proquotes/RestService/ProQuoteServiceJVC/GetDealerShiptoListJSon(066,305082)";
$(document).ready(function () {
$.ajax({
crossDomain: true,
cache: false,
type: "GET",
async: false,
dataType: "text",
contentType: 'text/plain',
url: GetDealerShiptoListJSon,
success: function (data) {
data = data.replace('<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">', '');
data = data.replace('</string>', '');
var finaldata = data;
var dataParsed = JSON.parse(finaldata);
var shiptosParsed = JSON.parse(dataParsed.Data );
var ShipToName = shiptosParsed.Shiptos[0].C ity;
$("#result").append(ShipTo Name);
},
error: function (xhr, status, error) {
// alert(xhr.responseText + " : error SelectDealerShipTo");
}
});
});
}
</script>
BURNSVILLE
but it supposes to return BURNSVILLE 2 times. Can you show me how to loop after the following statement?
var shiptosParsed = JSON.parse(dataParsed.Data
var ShipToName = shiptosParsed.Shiptos[0].C
Thanks,
<script type="text/javascript">
function GetDealerShiptoList() {
var GetDealerShiptoListJSon = "http://clientaccesstest.kenwoodusa.com/wcf/proquotes/RestService/ProQuoteServiceJVC/GetDealerShiptoListJSon(066,305082)";
$(document).ready(function
$.ajax({
crossDomain: true,
cache: false,
type: "GET",
async: false,
dataType: "text",
contentType: 'text/plain',
url: GetDealerShiptoListJSon,
success: function (data) {
data = data.replace('<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">', '');
data = data.replace('</string>', '');
var finaldata = data;
var dataParsed = JSON.parse(finaldata);
var shiptosParsed = JSON.parse(dataParsed.Data
var ShipToName = shiptosParsed.Shiptos[0].C
$("#result").append(ShipTo
},
error: function (xhr, status, error) {
// alert(xhr.responseText + " : error SelectDealerShipTo");
}
});
});
}
</script>
I try with following code, and it works just fine:
$.ajax({
url: 'http://clientaccesstest.kenwoodusa.com/wcf/orders/RestService/ProQuoteServiceJVC/GetDealerShiptoListJSon(066,305082)',
type: "GET",
dataType: "json",
crossDomain: true,
contentType: "text/plain",
success: function (msg) {
var mainJSONObject = $.parseJSON(msg);
var shiptosParsed = $.parseJSON(mainJSONObject.Data);
alert(shiptosParsed.Shiptos[0].ShiptoName);
var shiptoNames = [];
$(shiptosParsed.Shiptos).each(function() { shiptoNames.push(this.ShiptoName); });
alert(shiptoNames.length);
},
error: function (jqXHR, textStatus, errorThrown) {
alert('failed: ' + textStatus + ' - ' + errorThrown + ' - ' + jqXHR.responseXml);
},
});
ASKER
it is not working. and please use my sample. I want to get city and which is supposed to return twice.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I rewrote the codes and it works in Chrome/Firefox but not working in IE. No idea...any helps?
<script type="text/javascript">
function GetDealerShiptoList() {
var companyID = "066";
var dealerNo = "315158";
var GetDealerShiptoListJSon = "http://....GetDealerShiptoListJSon";
$(document).ready(function () {
$.ajax({
crossDomain: true,
cache: false,
type: "GET",
async: false,
dataType: "text",
contentType: "text/plain",
url: GetDealerShiptoListJSon + encodeURIComponent("(" + companyID + "," + dealerNo + ")"),
success: function (data) {
data = data.replace('<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">','');
data = data.replace('</string>','');
var finalData = data;
var dataParsed = JSON.parse(finalData);
var shiptosParsed = JSON.parse(dataParsed.Data);
for (i = 0; i <= shiptosParsed.Shiptos.length; i++) {
var cityName = shiptosParsed.Shiptos[i].City;
var stateName = shiptosParsed.Shiptos[i].State;
var shipToName = shiptosParsed.Shiptos[i].ShiptoName;
$("#SelectDealerShipTo").append("<option>" + shipToName + ", " + cityName + ", " + stateName + "</option>");
}
},
error: function (xhr, status, error) {
alert(xhr.responseText + " : error SelectDealerShipTo");
}
});
});
}
</script>
What error did you get in IE (using F12 - Developer Tools)?
ASKER
https://www.experts-exchange.com/questions/28656796/jquery-xml.html
Another question if you have time
Another question if you have time
Open in new window
The value you seek would be in the variable ShipToName.