Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

142
Views
Necesito enviar una solicitud a través de JS a un archivo php a través de API

Ayudame por favor. Hay dos archivos php Data.php y Status.php. Cuando ingresa datos en el campo zip, debe enviar una solicitud al archivo Data.php, y si zip está disponible, envíe los datos a Status.php ingrese el código aquí y analice la respuesta en el campo. js y Data.php, Status.php estaré agradecido por la ayuda)

 function ajax(params) { var xhr = new XMLHttpRequest(); var url = params.url || ''; var body = params.body || ''; var success = params.success; var error = params.error; xhr.open('POST', url, true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send(body); xhr.onload = function () { if (xhr.readyState === 4 && xhr.status === 200 && typeof success === 'function') { success(xhr.response); } else if (xhr.readyState === 4 && xhr.status !== 200 && typeof error === 'function') { error(xhr.response); } }; xhr.onerror = error || null; } //Data.php <?php header('Content-Type: application/x-www-form-urlencoded'); header('Access-Control-Allow-Origin: *'); if (isset($_POST['zip'])) { $zip = filter_var($_POST['zip'], FILTER_VALIDATE_REGEXP, array('options'=>array('regexp'=>'/^[0-9]{5}/'))); if ($zip) { $status = (int) $zip < 33333 ? array('zip' => $zip, 'state' => 'OH', 'city' => 'NEWTON FALLS') : array('zip' => $zip, 'state' => 'CA', 'city' => 'BEVERLY HILLS'); echo json_encode($status); } else { echo 'error'; } } else { echo 'error'; } //Status.php <?php header('Content-Type: application/x-www-form-urlencoded'); header('Access-Control-Allow-Origin: *'); if (isset($_POST['zip'])) { $zip = filter_var($_POST['zip'], FILTER_VALIDATE_REGEXP, array('options' => array('regexp' => '/^[0-9]{5}/'))); if ($zip) { $status = (int) $zip < 33333 ? 'allowed' : 'blocked'; echo $status; } else { echo 'error'; } } else { echo 'error'; }

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Debe enviar una llamada AJAX desde el primer php al segundo php.
Incluya el siguiente script dentro del primer archivo php.
prueba1.php

 <?php // other content <script> (function() { var httpRequest; document.getElementById("ajaxButton").addEventListener('click', makeRequest); function makeRequest() { httpRequest = new XMLHttpRequest(); if (!httpRequest) { alert('Giving up :( Cannot create an XMLHTTP instance'); return false; } httpRequest.onreadystatechange = alertContents; httpRequest.open('GET', 'test2.php'); httpRequest.send(); } function alertContents() { if (httpRequest.readyState === XMLHttpRequest.DONE) { if (httpRequest.status === 200) { alert(httpRequest.responseText); // your response } else { alert('There was a problem with the request.'); } } } })(); </script> ?>

Luego devuelva sus datos de contenido del siguiente archivo php de la siguiente manera.
prueba2.php

 <?php $x = "content data"; echo $x; ?>

Para obtener más detalles sobre AJAX, siga el siguiente enlace https://developer.mozilla.org/en-US/docs/Web/Guide/AJAX/Getting_Started

about 4 years ago · Juan Pablo Isaza Report

0

código JavaScript

 const data = { name: 'scott' }; // data for post fetch('url', { method: 'POST', headers: { 'Content-Type': 'application/json', // type }, body: JSON.stringify(data), }) .then(response => response.json()) .then(data => { console.log(data); }) .catch((error) => { console.error(error); });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!