I want to retrieve data from local storage and save it to the database. But when I'm trying to post, It gives an error 404. I know the file exist because when I remove this line :
formData.append('data',array);
my post is work. However, It works on localhost not on live server.
Whats wrong with this code.
Data from localstorage :
{
"mylist":"name of list",
"list": {
"category":[ {
"name":"Category 1",
"items":[ {
"name": "Category item list 1, "amount": "0"
}
]
}
,
{
"name":"Category 2",
"items":[ {
"name": "Category item list 2, "amount": "0"
}
]
}
]
}
}
React js trigger post :
function savepackList(){
const array = localStorage.getItem('list');
let formData = new FormData();
formData.append('type','savelist');
formData.append('data',array); //removing this it does not give a 404 error
axios.post('/request.php',formData)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});return;
}
PHP code :
if($_POST['type']=='savelist'){
$obj = json_decode($_POST['data'],true);
echo json_encode($obj);
}
Related
I am trying to populate a already built php database using axios requests, however i keep receiving 422 error and I don't understand what I am missing. Could you please help me :)
This is the error that i get:
xhr.js:177 POST URL/data 422 (Unprocessable Entity)
This is the Post request schema of the DB:
"post": {
"summary": "Post new data row",
"description": "Post new data row",
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"data": {
"type": "string",
"example": "{\"test\":1}"
},
"type": {
"type": "string",
"example": "1"
},
"status": {
"type": "integer",
"example": 1
}
},
"required": [
"data",
"type"
]
}
}
}
},
"responses" :{
"422": {
"description": "Error",
"content": {
"application/json": {
"schema": {
"type": "array",
"items": {
"type": "object",
"properties": {
"error": {
"type": "string",
"example": "Invalid input"
}
This is my code,
I have several switch cases and they should all work in the same manner:
case "rezervariContact" : {
const {type, titlu, description, phone, email} = this.state;
const contactData = {
type: this.state.type,
data:{
type,
data :{
titlu, description, phone, email
}
},
status:true
}
data = {...contactData};
}
}
await axios({
method: "post",
headers: { "Content-Type": "application/json",
'Accept': '*/*' },
body:data,
url : 'http://xxxxxxxxx.ro/data'
})
.then(function (response) {
console.log("RespResolved::",response.config.params);
})
.catch(function (response) {
console.log("catchErrResp::",response);
});
}
Please if you can spot something let me know.
422 probably means your inputted data is invalid.
The reason for this depends on the server. You could try referring to any documentation for the API/server you are trying to reach.
Check document on http://xxxxxxxxxx.ro/data! It means your post data is valid but server can not hand it correctly!
I am trying to get a json result to an array object .I am echoing a json data in php. like this.
<?php
header('Access-Control-Allow-Origin: *');
header('Content-type:application/json;charset=utf-8');
$arr= '[{
"id": "1",
"date": "2020-03-21",
"status": "present",
"studentid": "1"
},
{
"id": "2",
"date": "2020-03-24",
"status": "present",
"studentid": "1"
},
{
"id": "3",
"date": "2020-03-25",
"status": "absent",
"studentid": "1"
},
{
"id": "4",
"date": "2020-03-26",
"status": "absent",
"studentid": "1"
}
]';
echo $arr;
?>
~
How to get absentees using angular in an array.
Angular part i tried didnt work
this.http.post("http://localhost/android/Api.php?apicall=getattendance", JSON.stringify(this.postData),options)
.subscribe( (data) => {
this.setUsersArray(data);
console.log(data + "URL DATA"+JSON.stringify(this.postData));
}
);
=====================================================================
setUsersArray(data){
if (data instanceof Array) {
{
this.date_present = data.map(function (ele) {
if(ele.status==='present')
{
return ele.date;
}
});
this.date_absent = data.map(function (ele) {
if(ele.status==='absent')
return ele.date;
});
}
}
I am getting date_absent and date_present as null.Why am i getting this as null.Please help me. I am new to angular.
try for loop in typescript
setUsersArray(data:any){
for(let item of data){
console.log(item);
if(item.status==='present') {
this.present.push(item.date);
}
if(item.status==='absent') {
this.absent.push(item.date);
}
}
Trying to this url
$characters = json_decode($data, true); // decode the JSON feed and make an associative array
https://www.taniarascia.com/how-to-use-json-data-with-php-or-javascript/
function ajax()
{
$('form').submit(function() {
console.log($(this).serializeArray());
$('#result').text(JSON.stringify($(this).serializeArray()));
return false;
});
}
After this form I'm getting the json data:
[
{
"name": "firstName",
"value": "fsdfdf"
},
{
"name": "lastName",
"value": "df"
},
{
"name": "emailAddress",
"value": "refdfdfd.56#gmail.com"
},
{
"name": "password",
"value": "fdfdddd"
},
{
"name": "phoneNumber",
"value": "fdfdf"
}
]
I'm sending it to the server by this response.php
<?php
header('Access-Control-Allow-Origin: *');
$json = "http://ec2-54-201-121-123.us-west-2.compute.amazonaws.com:8080/refer247/registration";
$jsonfile = file_get_contents($json);
var_dump($jsonfile);
echo json_encode($_POST);
var_dump(json_decode($jsonfile));
echo json_decode($jsonfile);
?>
But after this I am getting json parser error. What actually is happening? I don't know. When I include datatype as text then my success function is calling but I want to send to the server only json data. What I'm doing wrong here...?
I appreciate if i get some help. Thanks.
Hi i have been trying to pull data in JSON format. Here is my code
$resourse_url = 'http://www.livepicly.com/app/api.php?method=list_vendor_name';
$json_data = file_get_contents($resourse_url);
$json_output = json_decode($json_data, TRUE);
$vendor = $json_output['vendor_name'][1];
echo "<pre>";
print_r($vendor);
exit(1);
However when i run the code it does not return anything. When i try to access the source URL via firefox, it asks me where to save the JSON data as *.php, however when i access the source URL from Chrome it displays the JSON data as normal.
What do i have to do here? Can anyone point me to a solution? Thx
running the returned json on http://jsonformatter.curiousconcept.com/
{
"result":[
{
"vendor_id":"726",
"vendor_name":"Scusa"
},
{
"vendor_id":"519",
"vendor_name":"Emilie French Restaurant and Bar"
},
{
"vendor_id":"482",
"vendor_name":"Cassis French Fine Dining"
},
{
"vendor_id":"435",
"vendor_name":"Asuka Japanase Dining"
},
{
"vendor_id":"12050",
"vendor_name":"Taipan"
},
{
"vendor_id":"12061",
"vendor_name":"Social House"
},
{
"vendor_id":"12103",
"vendor_name":"Harum Manis Indonesian Restaurant"
},
{
"vendor_id":"12193",
"vendor_name":"Nanny\'s Pavillion (Central Park)"
},
{
"vendor_id":"12272",
"vendor_name":"Bistro Baron"
},
{
"vendor_id":"20704",
"vendor_name":"Kitchenette (Central Park)"
},
{
"vendor_id":"21217",
"vendor_name":"Kitchenette (Plaza Indonesia)"
},
{
"vendor_id":"29859",
"vendor_name":"Momento Restaurant And Bar"
},
{
"vendor_id":"31055",
"vendor_name":"Tortuga Kitchen And Bar"
},
{
"vendor_id":"31056",
"vendor_name":"Tuck And Chug"
},
{
"vendor_id":"31060",
"vendor_name":"AUROZ Gourmet Grill"
}
]
}
it found invalid characters on this line
"vendor_name":"Nanny\'s Pavillion (Central Park)"
All,
I have an AJAX request, which makes a JSON request to a server, to get the sync status. The JSON Request and responses are as under: I want to display a JQuery UI progressbar and update the progressbar status, as per the percentage returned in the getStatus JSON response. If the status is "insync", then the progressbar should not appear and a message should be displayed instead. Ex: "Server is in Sync". How can I do this?
//JSON Request to getStatus
{
"header": {
"type": "request"
},
"payload": [
{
"data": null,
"header": {
"action": "load",
}
}
]
}
//JSON Response of getStatus (When status not 100%)
{
"header": {
"type": "response",
"result": 400
},
"payload": [
{
"header": {
"result": 400
},
"data": {
"status": "pending",
"percent": 20
}
}
]
}
//JSON Response of getStatus (When percent is 100%)
{
"header": {
"type": "response",
"result": 400
},
"payload": [
{
"header": {
"result": 400
},
"data": {
"status": "insync"
}
}
]
}
Assuming you want your progress bar/message to be placed in a div named "loadingDiv":
$(document).ready(function() {
var myLoadingDiv = $("#loadingDiv");
myLoadingDiv.progressbar({disabled:true});
$.getJSON("getStatus.php", function(data) {
if (data.payload.data.status == "insync") {
myLoadingDiv.progressbar("disable");
myLoadingDiv.html("Server is in Sync");
}
else if (data.payload.data.status == "pending") {
myLoadingDiv.progressbar("enable");
myLoadingDiv.progressbar("value", data.payload.data.percent);
}
else {
//something else if there are any other status'
}
});
});