-1

I have this code that captures the values of my API but I need these codes to be sent to the backend so that every time I open this script it sends it to the database



<script>
  const fpPromise = import('https://fpcdn.io/v3/Xhk2W83F3F8jStNRz99k')
    .then(FingerprintJS => FingerprintJS.load());



fpPromise
  .then(fp => fp.get({ extendedResult: true }))
  .then(result => {
    fetch("http://192.168.0.15/Visitantes_Verifica%C3%A7%C3%A3o/info.php", { method: "POST", body: JSON.stringify(result) });
  });


</script>

I'm trying to use this code to capture the values but it's not working

This is my info.php

<?php


header('Content-Type: application/json');


$dadosVisitante = json_decode(file_get_contents("php://input"), true);
//some code
echo json_encode($dadosVisitante);


?>

  • 2
    ...and the problem is, precisely? "I need" isn't a question and "how to" is a bit vague. See [ask] please. – ADyson May 31 '22 at 20:31
  • Try axios, just reading the docs would probably teach you how to do what you want to do. https://axios-http.com/docs/post_example – vfioox May 31 '22 at 20:32
  • P.S. fetch() doesn't have an option called "data". Read the documentation: https://developer.mozilla.org/en-US/docs/Web/API/fetch (and don't get it confused with the jQuery $.ajax function). https://stackoverflow.com/questions/29775797/fetch-post-json-data is probably also going to be useful... – ADyson May 31 '22 at 20:32
  • @ADyson the problem is how i can comunicate with my php to get this values? – Jones Moura May 31 '22 at 20:46
  • Well you're trying to send the values to php, but as I pointed out you're setting the wrong option. Try using `body` instead of `data`, and using the correct json header. Did you read the documentation and example I linked you to? – ADyson May 31 '22 at 20:53
  • @ADyson yes i changed to body and put the right header , but i still dont know how pass to backend – Jones Moura May 31 '22 at 20:56
  • Well that _is_ how you pass it to the backend. Are you actually asking how to write some php code to _receive_ the data? – ADyson May 31 '22 at 21:01
  • yes inded look : – Jones Moura May 31 '22 at 21:03
  • `This is my info.php`...the `fetch()` request goes to index.php, not info.php! – ADyson May 31 '22 at 21:13
  • `file_get_contents("http://192.168.0.15/Visitantes_Verifica%C3%A7%C3%A3o/index.php")` makes no sense - that will trigger another HTTP request to index.php! You need `file_get_contents("php://input")` to read from the input going into the current script execution - see the duplicate (in the blue box above your question). – ADyson May 31 '22 at 21:14
  • For future reference: "Not working" isn't an error message or a useful problem statement. We can't fix "not working" in code, any more than a mechanic can fix a car that is "not working", without any other information about the problem. How is it not working? What debugging have you done? What exactly happens when you run the code? What did you expect to happen instead? You should always provide details of error messages, unexpected behaviour etc. See also [What do you mean "It doesn't work"?](https://meta.stackexchange.com/questions/147616/what-do-you-mean-it-doesnt-work). – ADyson May 31 '22 at 21:15
  • @ADyson aIl fixed the errors you were talking about take a look at the code again. – Jones Moura May 31 '22 at 21:33
  • @ADyson I didn't mean to sound stupid but I just want to throw this into my php – Jones Moura May 31 '22 at 21:33
  • Ok. So what problem are you having now, exactly? Of course you won't see anything which PHP echoes back unless you go to your browser's Network tool and look at the response to the request going to info.php (you must click on the request, and then go to the Response tab) – ADyson May 31 '22 at 21:37
  • @ADyson i just want save that in my mysql – Jones Moura May 31 '22 at 22:00
  • Ok. And what is stopping you from doing that? You know how to make a SQL query, I hope? – ADyson May 31 '22 at 22:01
  • @ADyson I do i just dont wanna put the values of my json_decode in my database – Jones Moura May 31 '22 at 22:13
  • `i just dont wanna put the values of my json_decode in my database`...so what _do_ you want to put in the database, then? It's still unclear what the issue is or where you are stuck. – ADyson May 31 '22 at 22:15
  • @ADyson this array called result have so much results like – Jones Moura May 31 '22 at 23:10
  • @ADyson like browserName: "Chrome" ip: "177.192.25.40" ipLocation: {accuracyRadius: 100, latitude: -22.9485, longitude: -43.3436, postalCode: '20000', timezone: 'America/Sao_Paulo', …} lastSeenAt: {global: '2022-05-31T22:24:48.039Z', subscription: '2022-05-31T22:24:48.039Z'} meta: {version: 'v1.1.569+3e959d13'} os: "Windows" osVersion: "10" requestId: "1654036206907.umqWDO" visitorFound: true visitorId: "X7s5z975STkb4OoBUzRy" – Jones Moura May 31 '22 at 23:19
  • OK...and? Again, what's the problem? – ADyson May 31 '22 at 23:28

0 Answers0