I have recently taken over a project, and there is one thing i don't get about it.
Somehow there is a .js file that can change the names and values of the variables and such by using the # symbol. this is somehow called via the window object, for example i have one of the .js files with some ajax call in it:
var values#TEMPLATE_CONTROL_ID;
var regtemp#TEMPLATE_CONTROL_ID = #REGISTRATION_TEMPLATE_ID;
function save#TEMPLATE_CONTROL_ID(){
if (isRegistration#TEMPLATE_CONTROL_ID){
SaveInput#TEMPLATE_CONTROL_ID();
var toGoValues = ConvertRegistrationToGoValues#TEMPLATE_CONTROL_ID();
$.ajax({
url: 'scripts/SaveStandard.php',
type: 'post',
data: {'arrvalues': toGoValues, 'regID': registrationID#TEMPLATE_CONTROL_ID},
cache: false,
success: function(json) {
ImSaved();
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
}
else{
values#TEMPLATE_CONTROL_ID = new Array();
SaveInput#TEMPLATE_CONTROL_ID();
AddToGoArray#TEMPLATE_CONTROL_ID();
$.ajax({
url: 'scripts/SaveStandard.php',
type: 'post',
data: {'arrvalues': values#TEMPLATE_CONTROL_ID, 'regID': 1},
cache: false,
success: function(json) {
ImSaved();
},
error: function(xhr, status, error) {
var err = eval("(" + xhr.responseText + ")");
alert(err.Message);
}
});
}
}
This is somehow called with window[value](); where value could hold something like 1084. 1084 is entered where it says #TEMPLATE_CONTROL_ID.
My Question is does anyone recognise this, i figure that there should be some kind of map or something but i need to call something else, and would love to know how this works.
Related
Good Day,
I try to assign to a variable the result of an Ajax request. I tried both of the requests below, with no result (got an "Undefined" answer).
I don't understand the difference between:
var temp = obj.method(me, you);
and
var temp = (function () {
obj.method(me, you);
})();
Here is the AJAX request:
ObjID.prototype.getAjx = function(one, you){
$.ajax({
type: "post",
url: "./php/getAJX.php",
dataType: 'json',
context: this,
data: { one: one, two: two },
success: function(data) {
this.myProperty = data[0][0];
},
error:function(request, status, error) {
console.log("Something went wrong." + request.responseText + status + error);
}
});
}
Is this the same, or not ?
Thanks for your help! :)
The first two bits of code you showed effectively do the same thing.
However, neither of them do what you think they do. Neither of them are assigning the return value from your AJAX call anywhere:
The first line of your code:
ObjID.prototype.getAjx = function(one, you){
is simply assigning the function that contains the actual AJAX call to the .getAjx property of the ObjID.prototype. That function doesn't contain any return statements and upon execution, will not return anything. That is why you are getting undefined.
The code that actually performs the AJAX call is:
$.ajax({
type: "post",
url: "./php/getAJX.php",
dataType: 'json',
context: this,
data: { one: one, two: two },
success: function(data) {
this.myProperty = data[0][0];
},
error:function(request, status, error) {
console.log("Something went wrong." + request.responseText + status + error);
}
});
and, you aren't assigning that return value to anything. If you wanted to, you'd need to write something like this:
var result = $.ajax({
type: "post",
url: "./php/getAJX.php",
dataType: 'json',
context: this,
data: { one: one, two: two },
success: function(data) {
this.myProperty = data[0][0];
},
error:function(request, status, error) {
console.log("Something went wrong." + request.responseText + status + error);
}
});
JQuery AJAX calls return a reference to a Promise object and that's what you would be storing in that case.
I want to use POST method with AJAX in SAPUI5 javascript but I found an error.
var url = "https://xxxx*xxxx.co.id:8877/TaspenSAP/SimpanDosirPunah";
$.ajax({
type: "POST",
url: url,
data: JSON.stringify({
nomorDosir: "01001961288",
kodeCabang: "A02"
}),
dataType: "json",
async: false,
contentType: 'application/json; charset=utf-8',
success: function(data, textStatus, xhr){
console.log("sukses: " + data + " " + JSON.stringify(xhr));
},
error: function (e,xhr,textStatus,err,data) {
console.log(e);
console.log(xhr);
console.log(textStatus);
console.log(err);
}
});
error:
I already did change code with dataType=text, or data: {nomorDosir: "01001961288", kodeCabang: "A02"} (without stringify), but I not yet find the solution. How to fix this problem?
Thanks.
Bobby
Not sure what your use case is but if you are trying to post to an oData service, it might be much easier to use SAPs createEntry method where the URL is the path to the model you want to post to and your JSON are the properties:
var oModel = new sap.ui.model.odata.v2.ODataModel("https://services.odata.org/V2/OData/OData.svc/");
//oModel should use your service uri
var url = "https://xxxx*xxxx.co.id:8877/TaspenSAP/SimpanDosirPunah";
oModel.createEntry(url, {
properties: {
nomorDosir: "01001961288",
kodeCabang: "A02"
}
}, {
method: "POST",
success: function(response) {
alert(JSON.stringify(response));
//do something
},
error: function(error) {
alert(JSON.stringify(error));
}
});
oModel.submitChanges();
What you have is wrong json format, you have:
data: JSON.stringify({nomorDosir: "01001961288", kodeCabang: "A02"}),
Which actually should be:
data: {"nomorDosir": "01001961288", "kodeCabang": "A02"},
Which then you don't need to do a json.stringify on, because it already IS a json format. Hope this will help you out.
Which you could also try is setting a variable outside like this:
var url = "https://xxxx*xxxx.co.id:8877/TaspenSAP/SimpanDosirPunah";
var json = {"nomorDosir": "01001961288", "kodeCabang": "A02"};
$.ajax({
type: "POST",
url: url,
data: json,
dataType: "json",
async: false,
contentType: 'application/json; charset=utf-8',
success: function(data, textStatus, xhr){
console.log("sukses: "+data+" "+JSON.stringify(xhr));
},
error: function (e,xhr,textStatus,err,data) {
console.log(e);
console.log(xhr);
console.log(textStatus);
console.log(err);
}
});
Using the following jQuery, how can I read through the values of the JSON that's returned? With it how it is, the jQuery doesn't even run as there is an error in: alert("A" + obj.sender[0]);
var session_id = $(this).find(".session_id").val();
$.ajax({
type: 'POST',
url: '../php/read.php',
dataType: "json",
data: {sesh_id: session_id},
success: function (response) {
var obj = jQuery.parseJSON(response);
alert("A" + obj.sender[0]);
},
error: function (response) {
alert("Err: " + response.status);
}
});
The value of response is:
[{
"sender":"email#example.com",
"details":"details1",
"date":"2017-01-04 16:11:04"
},
{
"sender":"someone#example.com",
"details":"details2",
"date":"2017-01-04 16:11:05"
},
{
"sender":"blah#example.com",
"details":"details3",
"date":"2017-01-04 16:11:06"
}]
The issue you have is that your index accessor is in the wrong place as obj is an array, not the sender property, so it should be obj[0].sender.
You also don't need to call JSON.parse() on the response, as jQuery does that for you automatically as you specified dataType: 'json'. Try this:
$.ajax({
type: 'POST',
url: '../php/read.php',
dataType: "json",
data: { sesh_id: session_id },
success: function (response) {
console.log("A" + obj[0].sender);
},
error: function (response) {
console.log("Err: " + response.status);
}
});
Finally, note that console.log() is much more preferable when debugging over alert() as it doesn't coerce data types.
I am trying to load a dataset from the URL "https://data.raleighnc.gov/resource/5ccj-g2ps.json" but it requires an API key. I have had no luck with D3 or Jquery.
How would I go about doing this so that I can load the dataset in Json format?
I have the following:
$.ajax({
url: "https://data.raleighnc.gov/resource/xce4-kemu.json",
type: "GET",
data: {
"$limit" : 5000,
"$$app_token" : "YOURAPPTOKENHERE"
}
}).done(data) {
alert("Retrieved " + data.length + " records from the dataset!");
console.log(data);
});
It says I have a misplaced "{" but I don't see where.
There are a lot of mistakes in your code...
$.ajax({
url: "https://data.raleighnc.gov/resource/xce4-kemu.json", // didn't you want to get another URL??!?
type: "GET",
dataType: "json",
data: {
"$limit" : 5000, // Does the API require the dollar signs? Looks weird.
"$$app_token" : "YOURAPPTOKENHERE" // Did you actually replace with your API key?
},
success: (data) => {
alert("Retrieved " + data.length + " records from the dataset!");
console.log(data);
},
error: (xhr, textStatus, errorThrown) => {
// error
}
});
Should be working.
Im using a mixture of php and javascript/jquery to scrape a websites prices from there webpage, there's no API so unfortunately I scrape the html page and pick up the prices/title from the html(I know this is a really risky way of doing it but its the only way).
Anyway, this is whats going on:
I pull in the external webpage using php file_get_contents()
This method is within a foreach loop, for every external page I want to grab data from.
This is my javascript.
<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript'>
console.log('page-{$i}');
$('.page-{$i}').find('.search_result_row').each(function(i, obj) {
//This will give us each individual apps details.
var appTitle = $(this).find('.title').text();
var appPrice = $(this).find('.search_price').text();
var appDiscount = $(this).find('.search_discount').text();
var appDetails = {
'appTitle' : appTitle,
'appPrice' : appPrice,
};
callAjax(appDetails);
var my_delay = 5000;
function callAjax(appDetails) {
$.ajax({
url: 'Upload.php',
type: 'POST',
data: appDetails,
dataType: 'JSON',
success:function(data) {
console.log(data);
},
error:function(jqXHR, textStatus, errorThrown) {
console.log('request failed ' + textStatus + errorThrown);
console.log(appDetails);
}
});
}
});
</script>
Everything works fine for the first URL, and for about half of the urls that I'm grabbing data from. The issue is SOME of the data being sent via ajax is returning the following error
Upload.php net::ERR_EMPTY_RESPONSE
Can any of you help?
Try this. You must stringify your details before you post them as json. This could be a reason for ERR_EMPTY_RESPONSE.
function callAjax(appDetails) {
appDetailsJsonString = JSON.stringify(appDetails);
$.ajax({
url: 'Upload.php',
type: 'POST',
data: {appDetailsJson: appDetailsJsonString},
dataType: 'json',
success:function(data) {
console.log(data);
},
error:function(jqXHR, textStatus, errorThrown) {
console.log('request failed ' + textStatus + errorThrown);
console.log(appDetails);
}
});
}