web-proxy for angularjs application - angularjs

I have to create a web-proxy script for my angularjs files because I got the error of CORS(Cross Origin Request Method) and I dont have any options to use Access Control Allow Origin because I cant make any changes to my server end.
My backend data is in java. So please someone tell me how to make a web-proxy for my angularjs application.
Or is there anyway to bypass the cors request from my browser.

A quick work around using foreach and json_decode.
If your print_r($json) comes in this format:
Array
(
[0] => Array
(
[studentid] => 5
[firstame] => jagdjasgd
[lastname] => kjdgakjd
[gender] => 1
[email] => dgahsdg#em.com
[fathername] => hashsdh
[mothername] => djhavshd
[birthday] => 2016-03-21
[address] => gafdhfadhs
[tenth] => 45.235
[twelfth] => 56.25
)
)
This will do the trick:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$json = json_decode(file_get_contents("php://input"), true);
//print_r($json);
$data =array();//Open blank array for student data
$num = array();//Open Blank array for number of student
foreach($json as $k => $v):
$num [] = $v; //number of student
if(is_array($v)){
foreach($v as $key=>$val):
$data[$key] = $val;//Student data
endforeach;
}
endforeach;
$row= count($num);//Put number of student in $row
for($i=1; $i<=$row; $i++){
$q = 'INSERT INTO table (`col1`)
VALUES($data['studentid'])';//Looping through sql statement
}
Hope this will help.

User json_decode with true params
$data = "{"studentid":"5","firstame":"jagdjasgd","lastname":"kjdgakjd","email":"dgahsdg#em.com"}";
$d = json_decode($data,true); // true means it will result in aaray
print_r($d);
$stdId = $d['studentid'];
$fname = $d['firstname'];
$lname = $d['lastname'];
$mail = $d['email'];
EDIT:
For multiple json data:
$data = '[
{
"0": "1",
"studentid": "1",
"1": "David",
"firstname": "David",
"2": "Beckham",
"lastname": "Beckham",
"3": "1",
"gender": "1",
"4": "david123#gmail.com",
"email": "david123#gmail.com",
"5": "Beckham",
"fathername": "Beckham",
"6": "Beckhamii",
"mothername": "Beckhamii",
"7": "2016-03-13",
"birthday": "2016-03-13",
"8": "dgasdhghasd\nkajsdgjaksdh\nkahdgjaksgdas",
"address": "dgasdhghasd\nkajsdgjaksdh\nkahdgjaksgdas",
"9": "58.25",
"tenth": "58.25",
"10": "62.25",
"twelfth": "62.25"
},
{
"0": "3",
"studentid": "3",
"1": "Chris",
"firstname": "Chris",
"2": "Gayle",
"lastname": "Gayle",
"3": "1",
"gender": "1",
"4": "chrisgayle#email.com",
"email": "chrisgayle#email.com",
"5": "Chris Potters",
"fathername": "Chris Potters",
"6": "Christine",
"mothername": "Christine",
"7": "2016-04-20",
"birthday": "2016-04-20",
"8": "adhafsdh\njgadahksgdkjas\njagdjahsdlkajsld\nkajsgdjlahsdlkas",
"address": "adhafsdh\njgadahksgdkjas\njagdjahsdlkajsld\nkajsgdjlahsdlkas",
"9": "87.587",
"tenth": "87.587",
"10": "98.256",
"twelfth": "98.256"
},
{
"0": "5",
"studentid": "5",
"1": "jagdjasgd",
"firstname": "jagdjasgd",
"2": "kjdgakjd",
"lastname": "kjdgakjd",
"3": "1",
"gender": "1",
"4": "dgahsdg#em.com",
"email": "dgahsdg#em.com",
"5": "hashsdh",
"fathername": "hashsdh",
"6": "djhavshd",
"mothername": "djhavshd",
"7": "2016-03-21",
"birthday": "2016-03-21",
"8": "gafdhfadhs\nagdkjashdas\ndjkahsdklsaj",
"address": "gafdhfadhs\nagdkjashdas\ndjkahsdklsaj",
"9": "45.235",
"tenth": "45.235",
"10": "56.25",
"twelfth": "56.25"
}
]';
$json = json_decode($data, true);
echo '<pre>';
foreach ($json as $key => $value) {
echo "StudentID: ".$value['studentid']."<br>";
}
Output:
StudentID: 1
StudentID: 3
StudentID: 5

Decode ur array like below..
$newarr= json_decode('urjsonstring');
extract($newarr);
$query="insert into stud values($studentid, $firstname,$lastname...)";

Related

React table with JSON subdocument data only

I have nested arrays in JSON object that comes from CosmosDB. I have 3 subdocuments in the JSON which I want to display that data into a react table for each subdocument. How can we map each subdocument to a React Table Rows?
Below is the screenshot that I am expecting.
below is JSON data
[
{
"id": "DD3",
"studydate": "DDD",
"studydescription": "DD3 Description",
"studyname": "DD3",
"table1": [
{
"no": "1",
"name": "DD3 Name",
"date": "Krishna",
"description\r": "1111\r"
},
{
"no": "2",
"name": "DD3 Nam2",
"date": "Test2",
"description\r": "2222\r"
},
{
"no": "3",
"name": "DD3 Name3",
"date": "Test3",
"description\r": "3333"
}
],
"table2": [
{
"No": "2",
"Study Field1": "21",
"Study Field2": "22",
"Study Field3\r": "23"
}
],
"table3": [
{
"No": "3",
"Study Field5": "T31",
"Study Field6": "T32",
"Study Field7": "T33",
"Study Field 8\r": "T34"
}
],
"_rid": "QeNcANZFTTIKAAAAAAAAAA==",
"_self": "dbs/QeNcAA==/colls/QeNcANZFTTI=/docs/QeNcANZFTTIKAAAAAAAAAA==/",
"_etag": "\"33002e92-0000-0200-0000-5fa6fe320000\"",
"_attachments": "attachments/",
"_ts": 1604779570
}
]
I have a basic example of how you could generate the rows off JSON. I am just using basic HTML to keep it simple as the prop names will change depending what component you go with but same principle.
JavaScript for filtering and pushing to table array.
let rowArr = [];
Object.keys(data).forEach(function(key) {
rowArr.push(data[key])
})
HTML/JS
<table>
<!-- This should loop through rows allowing you to get row values -->
{rowArr.map(row => (
<tr key={row}>
<td>{row.Task}</td>
<td>{row.Description}</td>
<td>{row.Assignee}</td>
<td>{row.Client}</td>
<td>{row.Contact}</td>
</tr>
))}
</table>
Here is a simple JSON structure
let data =
{
1:{
Task: "Software",
Description: "Mockup designs",
Assignee: "John Doe",
Client: "Mark Wong",
Contact: "mark.wong#gmail.com"
}
}

React Native - Manipulate array when object have same id

I have an array named ticketCart and it store ticket that users add, if I have an objects that looks something like this with same id on it, how can I distinguish with other objects that have a same id?
ADDEDKEY Array [
Object {
"desc": "tunjukkan tiket saat masuk",
"idTicket": "47",
"name": "vip",
"price": "70000",
"quota": "1459",
"status": "Available",
"type": "PAID",
"value": 2,
},
Object {
"desc": "tunjukkan tiket saat masuk",
"idTicket": "47",
"name": "vip",
"price": "70000",
"quota": "1459",
"status": "Available",
"type": "PAID",
"value": 2,
},
]
I tried to do something like forEach on it but it doesn't work, id (number) is still the same (In here, I'm using number as an id)
ticketCart.forEach((o, key) => {
ticketCart[key].number = key + 1;
})
Thanks
What do you mean by distinguish? What are you trying to achieve?
For example, you could identify each object by it's index in the array:
for (let i = 0; i < ticketCart.length; i++) {
ticketCart[i].number = ticketCart[i].id + "_" + i;
}

Discord.js - JSON Parsing?

Okay, I'm a bit new to JSON in Discord.js Bot Development, And I'm currently making a command where it grabs all of the data in a json file and splits it so it looks nicely formatted.
Currently have:
let Games = {
"1": "Jailbreak",
"2": "Adopt Me",
"3": "Bubble Gum Simulator",
"4": "Thick Legends",
"5": "Arsenal",
"6": "Legends Of Speed",
"7": "Speed Champions",
"8": "Build A Boat For Treasure",
"9": "Boxing Simulator",
"10": "Flight Simulator",
"11": "Mad City",
"12": "Redwood Prison",
"13": "Horrific Housing",
"14": "Welcome To Bloxburg",
"15": "Tower Of Hell"
}
function getGames(lol) {
lol = JSON.parse(Games[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]);
for (let j in lol) {
return "• " + lol[j];
}
}
I want to group all of the Games and make it output:
"• example\n"
Any answers?
You can get all object values, use map to add • and join it with '\n'.
Like this:
let Games = {
"1": "Jailbreak",
"2": "Adopt Me",
"3": "Bubble Gum Simulator",
"4": "Thick Legends",
"5": "Arsenal",
"6": "Legends Of Speed",
"7": "Speed Champions",
"8": "Build A Boat For Treasure",
"9": "Boxing Simulator",
"10": "Flight Simulator",
"11": "Mad City",
"12": "Redwood Prison",
"13": "Horrific Housing",
"14": "Welcome To Bloxburg",
"15": "Tower Of Hell"
}
console.log(getGames(Games))
function getGames(gameList) {
return Object.values(gameList).map(game => `• ${game}`).join('\n')
}
Live example
let Games = {
"1": "Jailbreak",
"2": "Adopt Me",
"3": "Bubble Gum Simulator",
"4": "Thick Legends",
"5": "Arsenal",
"6": "Legends Of Speed",
"7": "Speed Champions",
"8": "Build A Boat For Treasure",
"9": "Boxing Simulator",
"10": "Flight Simulator",
"11": "Mad City",
"12": "Redwood Prison",
"13": "Horrific Housing",
"14": "Welcome To Bloxburg",
"15": "Tower Of Hell"
}
console.log(getGames(Games))
function getGames(gameList) {
return Object.values(gameList).map(game => `• ${game}`).join('\n')
}
You can get an array of the object values with Object.values()
let gamenames = Object.values(games)
Then,you can loop through the array elements to add them to a string.
let myString = "List: \n"
gamenames.forEach((val) => {
myString += `• ${val} \n`
})
myString is now equal to an organized list of the games! It should look like:
List:
• Jailbreak
• Adopt me
...etc..
(The list does put every single value on a seperate line,but StackOverflow doesn't want to show them properly for some reason)
Hope this helped! If it did,please mark my question as a valid answer.

Replace Property of de object ANGULARJS

I have an object and array from which I want to obtain a new object to show in the way below..
Group (Array)
this is the array and get of te services.
[
{
"IdQuestion": "5de12577fcfabc3c50660d36",
"idSurvey": "5de12546fcfabc3c50660d35",
"titleQuestion": "Porque compró el producto?",
"IdQuestionOption": "5de125b7fcfabc3c50660d39",
"idSurveyQuestion": "5de12577fcfabc3c50660d36",
"option": "Sabor"
},
{
"IdQuestion": "5de12585fcfabc3c50660d37",
"idSurvey": "5de12546fcfabc3c50660d35",
"titleQuestion": "Cómo se entero del producto?",
"IdQuestionOption": "5de125cbfcfabc3c50660d3c",
"idSurveyQuestion": "5de12585fcfabc3c50660d37",
"option": "Periodico"
},
{
"IdQuestion": "5de125a8fcfabc3c50660d38",
"idSurvey": "5de12546fcfabc3c50660d35",
"titleQuestion": "Con cuanta periodicidad consume el producto",
"IdQuestionOption": "5de125f8fcfabc3c50660d40",
"idSurveyQuestion": "5de125a8fcfabc3c50660d38", //the property is here.
"option": "Diario"
}
]
Result (Object)
this is the Object and i generate with select radios buttons with ng-models
{
"5de125a8fcfabc3c50660d38": "5de12601fcfabc3c50660d42", //the property is up.
"5de12585fcfabc3c50660d37": "5de125f0fcfabc3c50660d3f",
"5de12577fcfabc3c50660d36": "5de125c1fcfabc3c50660d3b",
"Age": "5",
"Gender": "1",
"IdSurvey": "5de12546fcfabc3c50660d35"
}
I need create a OBJECT
i need create this object for export to services
{
"Q03": "5de12601fcfabc3c50660d42",
"Q02": "5de125f0fcfabc3c50660d3f",
"Q01": "5de125c1fcfabc3c50660d3b",
"Age": "5",
"Gender": "1",
"IdSurvey": "5de12546fcfabc3c50660d35"
}
Solution 1
let res={
"5de125a8fcfabc3c50660d38": "5de12601fcfabc3c50660d42", //the property is up.
"5de12585fcfabc3c50660d37": "5de125f0fcfabc3c50660d3f",
"5de12577fcfabc3c50660d36": "5de125c1fcfabc3c50660d3b",
"Age": "5",
"Gender": "1",
"IdSurvey": "5de12546fcfabc3c50660d35"
}
res["Q03"] = res["5de125a8fcfabc3c50660d38"]
delete res["5de125a8fcfabc3c50660d38"]
res["Q02"] = res["5de12585fcfabc3c50660d37"]
delete res["5de12585fcfabc3c50660d37"]
res["Q01"] = res["5de12577fcfabc3c50660d36"]
delete res["5de12577fcfabc3c50660d36"]
Solution 2
let res={
"5de125a8fcfabc3c50660d38": "5de12601fcfabc3c50660d42",
"5de12585fcfabc3c50660d37": "5de125f0fcfabc3c50660d3f",
"5de12577fcfabc3c50660d36": "5de125c1fcfabc3c50660d3b",
"Age": "5",
"Gender": "1",
"IdSurvey": "5de12546fcfabc3c50660d35"
}
let changed={
"Q03": res["5de125a8fcfabc3c50660d38"],
"Q02": res["5de12585fcfabc3c50660d37"],
"Q01": "res["5de12577fcfabc3c50660d36"],
"Age": res.Age,
"Gender": res.Gender,
"IdSurvey": res.IdSurvey
}

How to filter Array in array key-value in AngularJS

I've the following structure.
[
{
"Variants": [
{
"SellPrice": "75.00",
"VariantID": "10",
"VariantName": "1 L",
"InCart": "2",
"MRP": "115.00",
"VariantImagePath": "/images/ruchi/710_10.png"
},
{
"SellPrice": "410.00",
"VariantID": "113",
"VariantName": "5 L",
"InCart": "1",
"MRP": "485.00",
"VariantImagePath": "/images/ruchi/710_113.png"
},
{
"SellPrice": "1080.00",
"VariantID": "219",
"VariantName": "15L - Jar",
"InCart": "0",
"MRP": "1275.00",
"VariantImagePath": "/images/ruchi/710_219.png"
}
],
"SubCategoryID": "32",
"ProductImagePath": "/images/ruchi/710.png",
"SubCategoryName": "Soyabean Oil",
"BrandName": "Ruchi",
"ProductID": "710",
"BrandID": "117",
"ProductName": "Ruchi soya oil"
},
{
"Variants": [
{
"SellPrice": "58.00",
"VariantID": "23",
"VariantName": "900 GM",
"InCart": "1",
"MRP": "60.00",
"VariantImagePath": "/images/mtr/771_23.png"
}
],
"SubCategoryID": "110",
"ProductImagePath": "/images/mtr/771.png",
"SubCategoryName": "Vermicelli",
"BrandName": "MTR",
"ProductID": "771",
"BrandID": "167",
"ProductName": "Seviyan Vermicelli"
}
]
Want to filter all the data where Variants.InCart value is > 0.
In this case output will be
ProductID VariantID InCart
710 113 1
710 10 2
771 23 1
and this is my loop.
<tr ng-repeat="Item in ProductService.Products | <what should be filter condition here>">
<td>{{Item.ProductID}} {{Item.Variants.VariantID}} {{Item.Variants.InCart}}</td>
</tr>
Please help.
<tr ng-repeat="Item in ProductService.Products | filter:customArrayFilter">
<td>{{Item.ProductID}} {{Item.Variants.VariantID}} {{Item.Variants.InCart}}</td>
</tr>
$scope.customArrayFilter = function (item) {
return (item.InCart > 0);
};
You can add custom filter like this..

Resources