This is my first time trying to deal with blob data.
I'm trying to send the blob image url via ajax to php for uploading the icon but it's not working, I am not sure if I'm even doing it correctly, can someone help me out?
console.log(apk.app.icon_url); // Prints: blob:http://localhost/9ca5a837-fdb4-4288-a21a-18e9650e6ac5
let data = new FormData();
data.append('icon', apk.app.icon_url);
$.ajax({
url: "testicon.php",
type: 'POST',
data: data,
contentType: false,
processData: false,
success: function(data) {
alert(data); // PHP array returns empty
}
});
Related
I'm having a problem trying to implement an AJAX request into a pre-existing website that I didn't create.
I have no problems sending the data to PHP as a GET request, however POST requests and trying to access $_FILES is returning null.
Here is the AJAX:
var someData = 'test';
$.ajax({
url: "post-data.php",
method: "POST",
dataType: "JSON",
data: ({someData}),
success: function(data) {
console.log(data);
}
});
PHP:
<?php echo json_encode($_POST['someData']); ?>
I believe the cause of the issue might lie within the htaccess file or be related to some other redirect that is in place on the site. This website was built by a past staff member at the company, and I've had this same problem using AJAX POST with several other sites built by them.
As changing from POST to GET works fine I don't think there is any problem with my very simple code.
Is there some way I can test if the data is going missing due to a redirect, or could there be some other cause?
First check the browser dev tools ctr+shift+J and see if there is a redirect. If not then
You need to set your datatype to json and depending on what version of JQUERY you are using you might have to use "type" instead of "method" if your JQUERY version < 1.9.0
var someData = 'test';
$.ajax({
url: "post-data.php",
method: "POST",
dataType: "json",
data: ({someData}),
success: function(data) {
console.log(data);
}
});
PHP code:
header("Content-Type: application/json", true);
If that doesnt work then make sure your URL is absolutely correct. No extra slashes or spaces.
I have managed to figure this out, there is a 302 redirect in place for all .php extetensions to a url without the extension. There was also an error in my code.
Changing the URL in the AJAX to "post-data" without the .php extension allows me to see $_POST in PHP.
My other problem with not being able to access files came down to the way I was trying to send FormData with AJAX.
My original code to do this was:
var someData = 'test';
var fData = new FormData();
fData.append("images", $("input[name='fileUpload']").prop("files")[0]);
$.ajax({
url: "post-data.php",
method: "POST",
dataType: "JSON",
contentType: false,
processData: false,
data: ({someData, fData}),
success: function(data) {
console.log(data);
}
});
The problem being that you can't send FormData and other variables at the same time. Instead I have to append my other data to the FormData:
var someData = 'test';
var fData = new FormData();
fData.append("images", $("input[name='fileUpload']").prop("files")[0]);
fData.append("someData", someData);
$.ajax({
url: "post-data.php",
method: "POST",
dataType: "JSON",
contentType: false,
processData: false,
data: fData,
success: function(data) {
console.log(data);
}
});
I have an ajax call, to upload multiple images from javascript. The code works well when I have a few images but as soon as I increase the files uploaded at the same time (ex: 530 files), the php does not receive anything. Any help to solve this issue please?
$("form").on("submit", function(e) {
e.preventDefault();
var formdata = new FormData(this);
formdata.append('regId', document.getElementById('register_id').value);
$.ajax({
url: 'uploadImages.php',
type: 'post',
data: formdata,
contentType: false,
processData: false,
success: function(response) {
document.getElementById("body").innerHTML = "<p>"+response+"</p>";
},
});
});
I tried to increase the max_upload_size and max_file_uploads from apache2 as this example:
https://connectwww.com/how-to-change-php-file-upload-size-limit-in-ubuntu/5087/
But still it does not work.
I'm using jstree on a project and attempting to save my tree to a database.
I'm obtaining the tree data as follows:
var tmp = $('#tree').jstree(true).get_json();
console.log(tmp);
This produces a JSON object in the console as I'd expect:
However when I post this to a PHP script using jquery...
$.ajax({
type: 'POST',
url: '/saveTree',
data: {'tree': tmp},
success: function(msg) {
console.log(msg);
}
});
... It is showing a PHP array of my data:
The script which I have at /saveTree displays the POST data in the tree array post key:
var_dump($this->request->data['tree']);
I assumed since the data I'm posting to the script is in JSON format I'd need to json_decode() it at the other end? If not, why not?
I've also tried adding dataType: 'json', in the ajax request but that makes no difference.
What's happening here?
Please note the PHP script at /saveTree is running in CakePHP 2.x so the line of PHP above is equivalent to var_dump($_POST['tree']) in regular PHP.
If you want send the data as string you can JSON.stringify(tmp);
tmp = JSON.stringify(tmp);
$.ajax({
type: 'POST',
url: '/saveTree',
data: {'tree': tmp},
success: function(msg) {
console.log(msg);
}
});
I have multiple forms generated dynamically.
I need to build the formData for multiple files as well as a couple of extra hidden inputs.
This is what I have tried so far:
$(document).on('click', '.upload-btn', function(e){
e.preventDefault();
var formData = new FormData($(this).parents('form')[0]);
$.ajax({
method: "POST",
url: "upload.php",
dataType: 'text',
cache: false,
contentType: false,
processData: false,
data: formData,
beforeSend: function(){
...
},
success: function(){
...
}
});
});
Oddly enough, this works somehow, and the files do get uploaded to the server, I can perform all sorts of operations on the temp files but...
the files are never a part of the $_POST array if I var_dump $_POST the files are not in there.
Is there a better way to build the formData? am I missing something on the php side when I'm testing for isset($_POST['file[]'])
Any help is greatly appreciated.
Use php's files array. print_r($_FILES);
Doing the above print, you'll be able to access every file sent from your form. This includes keys, tmp_name etc
I'm trying to upload a file via Ajax.
var fd = new FormData();
fd.append('file', file);
var xhr = $.ajax({
url: 'https://www.mywebsite.com/it/file/upload/',
type: 'POST',
dataType: 'json',
data: fd,
cache: false,
contentType: "application/json; charset=utf-8"
processData: false,
success: function(result, message, xhr)
{
console.log(result);
}
});
For the moment, the upload PHP script simply displays file data
header('Content-Type: application/json');
echo json_encode($_FILES['file']);
die();
As stated here, I am forced to use contentType:"application/json; charset=utf-8" because contentType:false causes a 404 (Not Found) error.
Unfortunately, this solution avoids the 404 error, but the displayed file data is null.
You should use this. It is working for me.
$.ajax({
type: 'POST',
url: $(this).attr('action'), //url_to_php_script
data: new FormData(this),
contentType: false,
processData: false,
success: function (data) {
//do necessary cleanups on success
},
error: function (e) {
//do necessary cleanups on failue
}
});
You will be able to get uploaded image in PHP using $_FILES
It should be ok the way you're doing it, however...
If you set contentType to false, than you will force jquery not to set the content type for you. If it is not set, it will default to `application/x-www-form-urlencoded; charset=UTF-8'.
That means you are sending your data using an url encoded string.
You can't easily send a file in an url encoded string.
Most likely the method on the server side is set up to read the form parameters from the url. Therefor if you set `contentType' to false, the server won't recognise the url, resulting in a 404.
Try setting up the server method to accept post data, remove the url parameters and read the posted form from the $_POST and $_FILES variables.
If you can't figure it out, update the question with the servers' method so we can see how the method is set up.