Update JQuery Progressbar with JSON Response in an ajax Request - php

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'
}
});
});

Related

How to fix error 422 on axios post request?

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!

[Dialogflow]Update Permission through Webhook format (for push notifications)

ORIGINAL REQUEST: I'm trying to implement the push notifications following the documentation: https://developers.google.com/actions/assistant/updates/notifications
I'm using Dialogflow with webhooks (in PHP) and the documentation is giving example in nodeJS
Right now, i'm blocked because of the Update permission, here's my Webhook response :
{
"source": "webhook",
"payload": {
"google": {
"expectUserResponse": true,
"systemIntent": {
"intent": "actions.intent.PERMISSION",
"data": {
"#type": "type.googleapis.com/google.actions.v2.PermissionValueSpec",
"permissions": [
"UPDATE"
]
},
"updatePermission": {
"intent": "notification.simple.text"
}
}
}
}
}
When I do the simulation, asks me a permission for a push, but not for the intent I specified.
I'm quiet sure that the problem is the updatePermission, something must be wrong with that:
Is it the field name?
In intent, i put the intent name that i filled in dialogflow, maybe do i have to an use action? Is it in the good format ?
If someone can help me or just give me an example of a clean response for an update permission.
Thanks!
Solution
I just found why, my json wasn't good, the updatePermissionValueSpec must be into data object.
{
"source": "webhook",
"payload": {
"google": {
"expectUserResponse": true,
"systemIntent": {
"intent": "actions.intent.PERMISSION",
"data": {
"#type": "type.googleapis.com/google.actions.v2.PermissionValueSpec",
"permissions": [
"UPDATE"
],
"updatePermissionValueSpec": {
"intent": "notification_simple_text"
}
}
}
}
}
}
I believe updatePermission should be named updatePermissionValueSpec.
Example response:
"payload": {
"google": {
"expectUserResponse": true,
"richResponse": {
"items": [
{
"simpleResponse": {
"textToSpeech": "PLACEHOLDER"
}
}
]
},
"systemIntent": {
"intent": "actions.intent.PERMISSION",
"data": {
"#type": "type.googleapis.com/google.actions.v2.PermissionValueSpec",
"permissions": [
"UPDATE"
],
"updatePermissionValueSpec": {
"intent": "intent_name"
}
}
}
}
}

PayPal how to pass the id to the client

I'm working on integrating PayPal to my site, however I've got to the point where it requests me to pass the id back to the client. And I'm not sure how to do this. Any help would be great.
This is the last JSON code I get from the PayPal servers. As I'm doing it from the server side code.
{
"id":"PAY-5VL741754E714161BLHUWCKY",
"intent":"sale",
"state":"created",
"payer":
{
"payment_method":"paypal"
},
"transactions":
[{
"amount":
{
"total":"4.00",
"currency":"USD",
"details":
{
"subtotal":"2.00",
"tax":"2.00",
"shipping":"1.00",
"shipping_discount":"-1.00"
}
},
"description":"The payment transaction description.",
"custom":"PlayerID",
"invoice_number":"merchant invoice",
"item_list":
{
"items":
[{
"name":"item 1",
"description":"item 1 description",
"price":"1.00",
"currency":"USD",
"tax":"1.00",
"quantity":1
},
{
"name":"item 2",
"description":"item 2 description",
"price":"1.00",
"currency":"USD",
"tax":"1.00",
"quantity":1
}]
},
"related_resources":
[]
}],
"create_time":"2017-10-20T02:36:27Z",
"links":
[{
"href":"h.t.t.p.s.:././.a.p.i...s.andbox.paypal.com/v1/payments/payment/PAY-5VL741754E714161BLHUWCKY",
"rel":"self",
"method":"GET"
},
{
"href":"h.t.t.p.s.:././.w.w.w...s.andbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-3U757031AX945661J",
"rel":"approval_url",
"method":"REDIRECT"
},
{
"href":"h.t.t.p.s:././.a.p.i...s.a.ndbox.paypal.com/v1/payments/payment/PAY-5VL741754E714161BLHUWCKY/execute",
"rel":"execute",
"method":"POST"
}
]}
You can use the json_decode() function (function docs) to decode the JSON information into a multi-dimensional array.
The following script will echo out the ID as required (provided $response is the JSON-encoded response from the PayPal transaction):
$data = json_decode($response, true);
echo $data['id'];
I worked it out, and its the following.
$response1 = json_decode($response);
$payment_id = $response1->id;
$payment_id = print_r($payment_id, true);
print ("{
\"id\": \"$payment_id\"
}");

Request object properties are empty, yet request is made

Creating a bolt extension and trying to access request variables. I have set up an endpoint GET /api/session, which for now should just return the request:
public function initialize()
{
$this->app->get("/api/session", array($this, 'getSession'))
->bind('getSession');
}
public function getSession(Request $request)
{
$response = $this->app->json(array('request' => $request));
return $response;
}
But the response is:
"request": {
"attributes": { },
"request": { },
"query": { },
"server": { },
"files": { },
"cookies": { },
"headers": { }
}
There were headers sent, and I've tested it with queries too, but nothing is returned in the response. How do I get the full response? Full code is available at: https://github.com/babaggeii/bolt-extension-restapi

Getting "json parser error" after sending form data to the server

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.

Categories