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

231
Views
La forma más limpia de sacar JSON de una devolución de función envuelta JSONP

Estoy trabajando con el servidor de medios Adobe Scene7, que molestamente devuelve un objeto de datos de imagen como JSONP, con un comentario y una función de ajuste.

Estoy usando .fetch() para realizar la solicitud y quiero obtener algunos datos de la respuesta.

Presionar la URL devuelve la siguiente estructura de respuesta:

 /*jsonp*/ s7jsonResponse({ "set": { /* image data here */ }}, "aCustomPassedIdentifier" );

Solo me importa el object devuelto por s7jsonResponse() pero estoy luchando por encontrar la mejor manera de analizar este JSONP.

Actualmente obtengo el text() de la respuesta cuando idealmente quiero json()

 fetch(url) .then((response) => response.text())

Y luego, con el texto de respuesta, tengo que usar str.replace para eliminar el comentario necesario y la función de ajuste:

 fetch(url) .then((response) => response.text()) .then((data) => { data = data .replace("/*jsonp*/", "") .replace("s7jsonResponse(", "") .replace(',"aCustomPassedIdentifier");', "") })

Y finalmente puedo analizar el texto restante como JSON:

 fetch(url) .then((response) => response.text()) .then((data) => { data = JSON.parse(data .replace("/*jsonp*/", "") .replace("s7jsonResponse(", "") .replace(',"aCustomPassedIdentifier");', "") ) })

Se siente como una forma increíblemente hacky de obtener lo que necesito. ¿Hay una mejor manera? ¿Hay algún método alternativo al que pueda llamar en response para obtener acceso solo al JSON sin las cosas adicionales?

about 4 years ago · Juan Pablo Isaza
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!