Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [AJAX]fetch AJAX wgrywanie obrazu, poproszę o rady i poprawki kodu.
casperii
post
Post #1





Grupa: Zarejestrowani
Postów: 681
Pomógł: 28
Dołączył: 14.08.2014

Ostrzeżenie: (0%)
-----


Witam , wracam z kolejnym błędem (pozdrawiam @nospor Lkingsmiley.png )
Poniżej prezentuje kod , niestety męczę się z banalnym problem sad.gif
Problem polega na tym, że nie przesyła obrazka / nie odbiera w pliku .php
Sam plik php jest 100% prawidłowy , katalog na serwerze ma prawidłowe chmody , problem tkwi gdzieś w podaniu prawidłowej wartości do przesłania.
Na konsoli błąd:
Uncaught (in promise) SyntaxError: Unexpected end of JSON input i tyczy się tej linii return response.json();

index.html
  1. const upload_image = (file) => {
  2.  
  3. if(!['image/jpeg', 'image/png'].includes(file.type))
  4. {
  5. document.getElementById('uploaded_image').innerHTML = '<div class="alert alert-danger">Z tym plikiem to wiesz gdzie możesz</div>';
  6.  
  7. document.getElementsByName('sample_image')[0].value = '';
  8.  
  9. return;
  10. }
  11.  
  12. if(file.size > 2 * 1024 * 1024)
  13. {
  14. document.getElementById('uploaded_image').innerHTML = '<div class="alert alert-danger">Masz za dużego plika , max to 2mb</div>';
  15.  
  16. document.getElementsByName('sample_image')[0].value = '';
  17.  
  18. return;
  19. }
  20. }
  21.  
  22. const form = document.getElementById('custom-step-news');
  23. form.addEventListener('submit', function(e){
  24. e.preventDefault();
  25.  
  26. const test = form.test.value;
  27. const sample_image = document.getElementsByName('sample_image')[0];
  28.  
  29. upload_image(sample_image.files[0]);
  30.  
  31. const form_data = new FormData();
  32. const file = document.getElementsByName('sample_image')[0].value = '';
  33. form_data.append('sample_image', file);
  34.  
  35.  
  36. fetch('xxupload.php', {
  37. method:"POST",
  38. headers: {
  39. 'Content-Type': 'multipart/form-data',
  40. },
  41. body:form_data
  42.  
  43. }).then(function(response){
  44.  
  45. return response.json();
  46.  
  47. }).then(function(responseData){
  48.  
  49. document.getElementById('uploaded_image').innerHTML = '<div class="alert alert-success">poszło</div> <img src="'+responseData.image_source+'">';
  50.  
  51. document.getElementsByName('sample_image')[0].value = '';
  52.  
  53. });
  54.  
  55. });


Ten post edytował casperii 14.07.2022, 16:55:09
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 21.08.2025 - 21:19