Handling file download from ajax get request in JavaScript - javascript

I am using a python CGI server to handle a cgi-bin request that I am making from an ajax call.
I'm trying to download the information I'm receiving on the Python side from my browser, using a button that triggers the ajax call.
I have tried some different things I saw on StackOverflow, but nothing has helped.
function issueDownloadCmd(port) {
var statusURL = window.location.protocol + "//" + window.location.host + "/cgi-bin/sod_log.py" + "?" + "down" + port.toString();
$.ajaxSetup({ 'cache': true });
$.ajax({
type: 'GET',
url: statusURL,
error: function (xhr, desc, exception) {
$('#traceStatus').html("Fail: response=" + xhr.status + "\nbecause: " + desc + "\nexception: " + exception);
},
success: function (data) {
console.log("data was downloaded");
}
});
return false;
}

Related

SOAP Request with WSSE-Header in Javascript

I have the following code to execute a SOAP request with an wsse header but it seems not to work. I get a error response but I am not able to get the fault/error out of the response.
Does the below code look correct or do I miss something?
var request = "<soapenv:Envelope" +
"xmlns:dlg='http://dlg123Lib'" +
"xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>" +
"<soapenv:Header>" +
"<wsse:Security" +
"xmlns:wsse='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd'" +
"xmlns:wsu='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd'>" +
"<wsse:UsernameToken wsu:Id='UsernameToken-BF09AFF37A2C36D4AB164000000000000000'>" +
"<wsse:Username>BLABLABLA</wsse:Username>" +
"<wsse:Password Type='http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText'>BLABLABLA</wsse:Password>" +
"</wsse:UsernameToken>" +
"</wsse:Security>" +
"</soapenv:Header>" +
"<soapenv:Body>" +
"<dlg:getData>" +
"<name>AB</name>" +
"</dlg:getData>" +
"</soapenv:Body>" +
"</soapenv:Envelope>"
jQuery.ajax({
url: 'https://endpoint_url/dlg/data',
type: "POST",
data: request,
dataType: "xml",
contentType: "text/xml; charset='utf-8'",
success: function (soapResponse) {
alert("Success");
},
error: function (soapResponse) {
alert("Error");
}
});
When I execute the exact same request like above in SoapUI without setting an Authorization the request is executed correctly.
So it must be something wrong with the jQuery implementation(?)
UPDATE 15.01.2022
I was told by the customer that the Payload of the request is empty. Anybody an idea why this could happen?

SOAP "POST" request (AJAX) in JavaScript (VUE) in Browser not giving right response

I have been trying to POST a SOAP request via $ajax (see below) to a remote web service. I am using unsecure Chrome and testing from localhost, the response is not what I am getting on SOAPUI.
I have tested the same service with the same parameters in SOAPUI and it works perfectly. In the browser I am getting a 200 code but getting an error code that is built in the web service. what am i doing wrong? does it have to do with the browser? AJAX?
data () {
return {
str: '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/">' +
'<soapenv:Header>' +
'<tem:ROClientIDHeader>' +
'<tem:ID>{E5D6D2B8-3624-4959-872A-E536B213DCA0}</tem:ID>' +
'</tem:ROClientIDHeader>' +
'</soapenv:Header>' +
'<soapenv:Body>' +
'<tem:LeerlingService___FindLeerling>' +
'<tem:Credentials>' +
'<tem:Username>TONATHIU</tem:Username>' +
'<tem:Password>tonathiu</tem:Password>' +
'</tem:Credentials>' +
'<tem:INSZNummer>8788041754941</tem:INSZNummer>' +
'<tem:Naam>Redroban</tem:Naam>' +
'<tem:Voornaam>Tonathiu</tem:Voornaam>' +
'<tem:GeboorteDatum>1988-04-17T00:00:00.000Z</tem:GeboorteDatum>' +
'<tem:EnkelZoekenOpINSZ>false</tem:EnkelZoekenOpINSZ>' +
'</tem:LeerlingService___FindLeerling>' +
'</soapenv:Body>' +
'</soapenv:Envelope>'
}
},
methods: {
soapRequest () {
$.ajax({
type: 'POST',
url: 'my web service',
data: this.str,
success: function (result) {
console.log(result)
},
contentType: 'application/xml',
dataType: 'text/xml;'
})
},
Problem solved!
<tem:GeboorteDatum>
This should have been <tem:Geboortedatum>. Always check what the back-end is looking for. The strangest part was that SoapUI was correcting that for me, and was not noticed.

Ajax character encoding changed after send to server

I have an ajax like this:
$.ajax('../EditUser?userId=' + editingId + '&full_name=' + name + '&position=' + position + '&office=' + office
+ '&office_address=' + office_address + '&age=' + age + '&user_login_name=' + user_login_name + '&email=' + email
+ '&user_type=' + usertype + '&password=' + password + '&meetingIds=' + selectedmeetingid, {
type: 'POST',
success: function (data) {
if (data.indexOf('error://') < 0) {
$('#tbl_meetings').html(data);
} else {
$('#errorMessage').html(data);
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error " + errorThrown);
alert("error " + textStatus);
alert("error " + jqXHR.status);
}
}
);
and my server received data with wrong encoding,example: "Hà Thị Minh Thắng" became "H? Th? Minh Th?ng" after received on server side.I tried adding
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
and
beforeSend: function(jqXHR) {
jqXHR.overrideMimeType('text/html;charset=iso-8859-1');
}
to my ajax but it didn't works. So, anyone know how to fix this?
Try specifying content type to the ajax request & you have to add header to the server side to receive the charset.
$.ajax({
data: parameters,
type: "POST",
url: your-url,
timeout: 20000,
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-1",
dataType: 'json',
success: callback
});
// Server Side... (This is an example in php which I have used in my app)
header('Content-Type: text/html; charset=ISO-8859-1');
Hope this helps you!

jQuery 1.8 ajax call returns null although post response has value

I'm having problems with the return value of a jQuery ajax call. I can debug the whole thing server side and I know everything is working correctly and the return value is properly being calculated. I can look under the NET tab in FireBug and see that the response is:
{"d":false}
But when I test the value in the Success function of the ajax call, msg is NULL. Why?
Here's the ajax call:
function GetStateCertifiable(areaID) {
$.ajax({
url: "../WebServices/AoP.asmx/GetStateCertifiable",
data: '{"AreaID":"' + areaID + '"}',
dataType: 'json',
success: function (msg) {
alert(msg); // for debugging
if (msg)
$("#isCertified").slideDown("fast");
else
$("#isCertified").slideUp("fast");
},
error: function (msg) {
alert("An error occured. \nStatus: " + result.status
+ "\nStatus Text: " + result.statusText
+ "\nError Result: " + result);
},
complete: function () {
}
});
};
Other, similarly structured client-side calls work fine. This is a same-domain request.
try changing the name of the variable to something other than msg. I think that might be a message box or something similar. Try
function GetStateCertifiable(areaID) {
$.ajax({
url: "../WebServices/AoP.asmx/GetStateCertifiable",
data: '{"AreaID":"' + areaID + '"}',
dataType: 'json',
success: function (result) {
alert(result); // for debugging
if (result)
$("#isCertified").slideDown("fast");
else
$("#isCertified").slideUp("fast");
},
error: function (result) {
alert("An error occured. \nStatus: " + result.status
+ "\nStatus Text: " + result.statusText
+ "\nError Result: " + result);
},
complete: function () {
}
});
};
It turns out the problem was that my web service (../WebServices/AoP.asmx/GetStateCertifiable) returned a bool and from the post response, I know that was properly sent back to the client. Ajax, however, didn't like that. Once I changed the web service to return the strings "true" or "false", everything worked.
Does jQuery ajax only work for strings or is there something I should have done to prepare the msg object to receive a bool?

Ajax call never gets called in phonegap for android

I searched similar questions but never found a proper answer. I am making app for multiple platforms using phonegap and JQM. I made index.html, which is "login page", with similar call. Index's call works, but the one below never gets called on my android device, even though it works both on chrome and safari.
I checked sever logs and there are no problems with "login", but as I said, there is no request from my android device when function below should be called.
//document.addEventListener("deviceready", onDeviceReady, true);
$( document ).bind( "mobileinit", function(){
$.support.cors = true;
$.mobile.allowCrossDomainPages = true;
$.mobile.loadingMessageTextVisible = true;
$.mobile.showPageLoadingMsg();
console.log('Page Started');
})
//var onDeviceReady = function(){
$( document ).ready(function (){
console.log('Start');
//$.support.cors = true;
//$.mobile.allowCrossDomainPages = true;
$.ajax({
crossDomain: true,
type: 'GET',
url: 'http://ip/services/rest/contact/list',
callback: 'jsonpCallback',
jsonpCallback: 'jsonpCallback',
jsonp: '_jsonp',
contentType: 'application/json',
dataType: 'jsonp json',
timeout : 10000,
success: function(data){
var html ='';
console.log('Success');
$.each(data.response, function(key, value) {
html += '<li><a class=contact href="#" id="' + data.response[key].id + '" ><h1>' + data.response[key].label + '</h1><p>'+ data.response[key].customerName + '</p><p>' + data.response[key].phone + ', ' + data.response[key].email + '</p></a></li>';
$('#ul_id').append($(html));
html='';
console.log('conatct');
});
$('#ul_id').trigger('create');
$('#ul_id').listview('refresh');
},
error: function (xhr, ajaxOptions, thrownError){
alert("Status: " + xhr.status + ", Ajax option: " + ajaxOptions + ", Thrown error: " + thrownError);
//location.reload();
console.log('Blad');
},
});
});
What is the access origin value in your Android project config.xml file ?
your value should be as below :
<access origin=".*"/>
There was a problem with placing script and specific way that jqm work.

Categories

Resources