How to access RESPONSE data from an AJAX Get in JQuery - ajax

This is my first attempt at using an AJAX GET to pull data and it is really causing me a nightmare. I have used POST loads of times and assumed accessing the data would be the same basic process, but no matter what I try I always end up with "Undefined".
This is the code I have been trying to make the GET work in my script;
$(document).ready(function() {
$.ajax({
type: "GET",
url: "/buytoken/public/api/stage?secret=***************",
cache: false,
dataType: "json",
success : function(response){
var len = response.length;
for(var i=0; i<len; i++){
var sold = response[i].progress;
}
alert(sold);
}
});
});
This is then what the response should be;
{
"success": true,
"response": {
"ico": "running",
"total": "8,000,000",
"total_amount": "800,000",
"sold": "1,599,300",
"sold_amount": "159,930",
"progress": 20,
"price": "0.1",
"start": "2022-02-28 00:01:00",
"end": "2022-05-31 00:01:00",
"timezone": "Europe/London",
"min": "500",
"max": "4000000",
"soft": "400000",
"soft_amount": "40,000",
"hard": "800000",
"hard_amount": "80,000"
}
}
The script that I am trying to access is on the same webserver.
It is coming from a third-party piece of software that we are running on the site. The documentation states "Application provides some specific internal live data in JSON"
So, we should be getting JSON back.
I need to access just two pieces of data "sold_amount" and "progress" which I want to load into variables in the Success callback.
I really would be grateful if somebody could explain or better still show me how this is supposed to work because I am really banging me head against the wall on this.
Also, if I need to add any extra bits like error handling then please point that out.
Thanks to anybody who responds to this..

Related

How to use strictFilters parameter for QnA in nodejs with botbuilder-ai sdk

I am able to use strictFilter parameter with normal rest API call to QnA Maker
{
"question": "Campaign and AEM integration",
"top": 3,
"strictFilters": [{
"name": "product",
"value": "adobe"
}]
}
But when I am trying to do the same thing using botbuilder-ai nodejs sdk, it is not providing me the desired result, I am pretty sure it is ignoring the strictFilters parameter.
Honestly the request is so simple I would just use REST instead of the sdk. I'm still using request which has be deprecated, but would work fine with axios or your module of choice. Here's mine where I'm not sending filters but I do have context which I send. Regardless this will function exactly the same if you make the call via Postman or within nodejs.
const qnaResult = await request({
url: url,
method: 'POST',
headers: headers,
timeout: process.env.DEFAULT_API_TIMOUT,
json: {
question: query,
top: 3,
context: qnAcontext
}
});

Shopify Predictive Search Ajax Call

I'm quite new to shopify, liquid and all that comes with it.
I try to implement predective search on shopify and I just don't know why I'm not even getting the alert of the Ajax call that is in the following code. I put it in my "theme.liquid" file right above the closing body tag. I assume if it would work, there should be the alert right when opening the page, no? Goki is a vendor, many products are shown when I search for it on my page. My code looks like this:
<script>
jQuery.getJSON("/search/suggest.json", {
"q": "goki",
"resources": {
"type": "product",
"limit": 4,
"options": {
"unavailable_products": "last",
"fields": "title,product_type,variants.title"
}
}
}).done(function(response) {
var productSuggestions = response.resources.results.products;
if (productSuggestions.length > 0) {
var firstProductSuggestion = productSuggestions[0];
alert("The title of the first product suggestion is: " + firstProductSuggestion.title);
}
});
</script>
Any help would be very very much appreciated! Thank you!
I solved it. I created an own variable AjaxData with all the 'resources' parameter before entering the JQuery call. There I don't use the variable to create the url, it's just used to define param in the call, not to change the url. -that's the trick

Ajax datatable Server-side processing draw

During server side processing, one of the parameters sent from the server "draw" is coming as null, but I am not able to figure out in which scenario is that parameter being sent as null from the server, has anyone encountered this issue before , i tried navigating though the application but I am not able to replicate this issue, got this issue only once, but not able to reproduce it.
Basically, we have a datatable in our application, in which the data gets populated through ajax call, but in one of the scenarious , the "draw" parameter is sent as null from the server.
This is the content of the ftl file(similar to HTML file) which is used server side processing :
"processing": true,
stateSave: true,
"serverSide": true,
"pageLength": 10,
"lengthMenu": [10, 25, 50, 100],"pagingType": "full_numbers",
"scrollX": "true","scrollY": 500,
"ajax": {
"url": "/platform/${registry}/facility${facilityId}/case-list/load",
"type": 'POST',
"data": function (d) {
d.visibleColumns = getVisibleColumns();
},
headers: {
'X-CSRF-Token': $('meta[name="_csrf"]').attr('content')
}
},
There is a code in the same ftl file where the application is manually sending the parameters to the server:
var url = "/platform/${registry}/facility${facilityId}/case-list/export?output=excel&draw=1&start=0&length=100000";
Though we are not doing anything with these paramerters in the controller class(Spring MVC contoller)
This is the code in the java class, which is trying to read this parameter value and parsing it to int , since "req.getParameter("draw")" value is null, its throwing NumberFormatException in the code, but not able to replicate in which scenario.
public int getDraw() {
return Integer.parseInt(req.getParameter("draw"));
}

Shopify order API returns OK but does not update order using JS

I'm trying to create an order using JS. I've authenticated my app and have a function that POST's to orders.json. I see a status code of 200, indicating that the request submitted OK (right?) but the order itself never gets created. I've heard something about disabling cookies in my request, but I don't know how to do that, so if that's what I need to do please let me know.
I'm putting up my entire function, since I'm new to this entire thing and it seems that it's probably the structure of my request, not the API call. I'm seeing the "Error" log in the console, so clearly the error function is running.
function submitShuffle(prodid)
{
console.log(prodid);
var params = {
"order": {
"line_items": [
{
"variant_id": prodid,
"quantity": 1
}
]
}
};
$.ajax({
type: 'POST',
url: 'https://<my-usrnam>:<my-pass>#toyboxshufflesandbox.myshopify.com/admin/orders.json',
dataType: 'application/json',
data: params,
success: function(data){
console.log(data);
},
error: function(data){
console.log("Error");
console.log(data);}
});
}
You cannot retrieve information from Shopify Admin API by AJAX. There is a limitation about this because you have to expose your username/key and password which is not a good idea.
You have to use some service/app or just to create a custom app.
Shopify returns an Object when you make a call. Hence 200 OK status. Inspect the object returned. A failed create POST object will not have an ID. So there is clue number one. Secondly, you'll see that Shopify tells you what the problem was in the Error key of the returned object. If you cannot figure out what you did with the message from Shopify, make the same call to the GraphQL endpoint if you can, as the error messages Shopify returns from those endpoints are currently much better. They are backporting them to the older REST API, but for now, GraphQL is more expressive.

How to make multiple calls synchronous in elasticsearch?

Im creating ajax calls to an elasticsearch server inside a "for" loop. The problem Im facing is that,the responses are not coming in proper sequence, (that is in the order which the clients are generated using for loop). How to make the calls and response synchronous? .
You could do something like this:
$(document).ready(function(){
function extendFuncChain(url, callback) {
func_chain_list.push(function() {
$.ajax({
url : url,
type: 'GET',
contentType:"application/json"
}).done(function(data) {
console.debug(data);
callback();
});
});
}
var func_chain_list = [function() {}];
for(var i=1; i<=4; i++) {
var url = "http://localhost:9200/test_index/doc/" + i;
var callback = func_chain_list.pop();
extendFuncChain(url, callback);
}
func_chain_list.pop()();
});
To test it I set up a simple index with 4 documents like this:
DELETE /test_index
PUT /test_index
POST /test_index/doc/_bulk
{"index":{"_id":1}}
{"name": "doc1"}
{"index":{"_id":2}}
{"name": "doc2"}
{"index":{"_id":3}}
{"name": "doc3"}
{"index":{"_id":4}}
{"name": "doc4"}
Then when I put the javascript in a web page and load it, I get this output in the console (I expanded the last one):
Hopefully you can see how to generalize this to do what you need. The call to extendFuncChain is important, because of the way closures work in JS, and it won't work if you take that out.

Resources