Upload files via Ajax and api key? - javascript

I have below code for post request but 422 error is shown:
$.ajax({
url: 'https://napi.arvancloud.com/vod/2.0/channels/166c3942-4e8d-4ab7-a543-c46e4528bf08/videos',
headers: {
'Authorization': authorization,
'Accept-Language': "en"
},
data: new FormData(document.forms[0]),
contentType: false,
processData: false,
type: 'post',
success: function() {
alert('Uploaded by jQuery');
}
})

$.ajax({
url: 'https://napi.arvancloud.com/vod/2.0/channels/166c3942-4e8d-4ab7-a543-c46e4528bf08/videos',
headers: {
'Authorization': authorization,
'Accept-Language': "en"
},
data: new FormData(document.forms[0]),
contentType: false,
processData: false,
type: 'post',
success: function () {
alert('Uploaded by jQuery');
}
})
});
})

Related

How to fix 403 (Forbidden) error when posting data from Javascript to Django

var upload = function(files) {
$.ajax({
type: 'POST',
processData: false,
contentType: false,
cache: false,
data: {
csrfmiddlewaretoken: '{{csrf_token}}',
files: files
},
enctype: 'multipart/form-data',
url: 'files/',
});
}
I try to send files to Django but I am getting this error.
jquery-3.4.1.min.js:2 POST http://localhost:8000/upload/files/ 403 (Forbidden)
you put the token in the header before you send it with ajax
$.ajax({
type: 'POST',
processData: false,
contentType: false,
cache: false,
beforeSend: function (xhr) {
xhr.setRequestHeader('X-CSRFToken', '{{csrf_token}}');
},
data: {"files":files},
enctype: 'multipart/form-data',
url: 'files/',
});

AJAX: How to get array on multi dimensional array

I have a json which is . I just want to get specific data which is
obj['contacts']['name']
How can i get
obj['contacts']['name']
name on Contacts array
This is my code:
$.ajax({
type: 'GET',
dataType: 'json',
url: uri,
cache: false,
contentType: 'application/json',
success: function(data) {
for (var obj in data) {
console.log(obj['contacts']['name']);
}
}
});
In your case this is how you want get name from contacts
$.ajax({
type: 'GET',
dataType: 'json',
url: uri,
cache: false,
contentType: 'application/json',
success: function(data) {
if (!data.contacts) return;
var names = data.contacts.map(function(dt) {
return dt.name;
});
console.log(names);
}
});
Just enumerate the returned object "contacts" property:
$.ajax({
type: 'GET',
dataType: 'json',
url: uri,
cache: false,
contentType: 'application/json',
success: function(data) {
data.contacts.forEach(function(contact) {
console.log(contact.name);
});
}
});

How to do a service call for 'POST' method?

How to do a service call for 'POST' method for the payload with the headers described below?
Payload for the service:
{
"SupplierPOPrint": {
"ErrorFlag": "Status Code: 20, Message ID: 505",
"ResultMessage": "Integration Service: Unable to find a routing corresponding to the incoming request message."
}
}
headers: { "Accept": "application/json", "Content-Type": "text/xml", },
function poPrint() {
$.ajax({
url: '/wps/proxy/http/10.249.114.31:8009/soa-infra/resources/Supp‌​lierPortal/CreateSup‌​plierPOPrintService!‌​1.0/CreateSupplierPO‌​PrintService/Create'‌​,
type: 'POST',
dataType: 'json',
data: new XMLSerializer().serializeToString(updatedTemplate),
crossDomain: true,
success: function(poPrintData) {
console.log("poPrintData", poPrintData);
poPrintDetails = poPrintData.SupplierPOPrint;
console.log("poPrintDetails", poPrintDetails);
poPrintTemplate = generatepoPrint(poPrintData);
$("#poPrintOutput").html(poPrintTemplate);
},
error: function(data) {
console.log("Update Content Failed", data);
}
});
}

jQuery ajax request can do XHR finished GET but not authorized?

$(document).ready(function() {
$.ajax({
type: "GET",
dataType: "json",
contentType: "text/plain",
xhrFields: {
withCredentials: true
},
url: 'http://kitabisa.xyz/api/v2/campaigns',
username: 'apps1',
password: 'XD6WVjhcq3JVrFRldrpjEpAyUXg5LFzS9cmiGuXL3TmE7bkLGR',
success: function(result) {
alert('done');
},
complete: 'callback'
})
});
I want to GET JSON from API via jquery Ajax function, above is my code snippet, but the CORS Policy block the ajax, here is the console message.
I've tried the another method, I use beforeSend ajax here :
$(document).ready(function() {
$.ajax({
type: "GET",
dataType: "jsonp",
contentType: "text/plain",
xhrFields: {
withCredentials: true
},
url: 'http://kitabisa.xyz/api/v2/campaigns',
beforeSend: function(xhr) {
xhr.setRequestHeader("Authorization", "Basic YXBwczE6WEQ2V1ZqaGNxM0pWckZSbGRycGpFcEF5VVhnNUxGelM5Y21pR3VYTDNUbUU3YmtMR1I=")
},
username: 'apps1',
password: 'XD6WVjhcq3JVrFRldrpjEpAyUXg5LFzS9cmiGuXL3TmE7bkLGR',
success: function(result) {
alert('done');
}
})
});
But still not works.
Any suggestion to fix this problem? Thank you
try the following
$.ajax({
type: "GET",
dataType: "json",
contentType: "text/plain",
xhrFields: {
withCredentials: true,
username: 'apps1',
password: 'XD6WVjhcq3JVrFRldrpjEpAyUXg5LFzS9cmiGuXL3TmE7bkLGR'
},
url: 'http://kitabisa.xyz/api/v2/campaigns',
success: function(result) {
alert('done');
},
});

jQuery Ajax header not being passed

I want to send an AJAX call to a service with a couple of custom headers. While the request goes successfully, the headers are not being sent. The following is my code snippet:
func123: function (params) {
return $.ajax({
url: "http://localhost:8080/",
type: 'GET',
headers: {
"Contract": "1",
"Vector": "abc",
"Authorization": "xyz"
},
dataType: 'json',
contentType: 'application/json'
});
},

Categories