Chirurgia endoscopică a hipofizei
"Standardul de aur" în chirurgia hipofizară îl reprezintă endoscopia transnazală transsfenoidală. Echipa NeuroHope este antrenată în unul din cele mai mari centre de chirurgie a hipofizei din Europa, Spitalul Foch din Paris, centrul în care a fost introdus pentru prima dată endoscopul în chirurgia transnazală a hipofizei, de către neurochirurgul francez Guiot. Pe lângă tumorile cu origine hipofizară, prin tehnicile endoscopice transnazale pot fi abordate numeroase alte patologii neurochirurgicale. www.neurohope.ro |
Accesarea unui var dintr-un script javascript
Last Updated: Sep 24 2020 19:11, Started by
mireazma
, Sep 24 2020 15:17
·
0
#1
Posted 24 September 2020 - 15:17
Salutare.
Am nevoie sa accesez o variabila dintr-un script de pe un server. Eventual sa o fac disponobila intr-un json object care e returnat la un request URL. Partea de client o am, trimite un GET URL si asteapta un obiect json. Ce nu gasesc desi am cautat pe google si experimentez de 3 ore este partea de server. Poate parea super simplu si asa ma gandesc ca ar si trebui sa fie. Dar nu gasesc. Incepand de la w3schools, tutoriale youtube pana la StackOverflow tot ce gasesc este cum fac requestul cand deja serverul pune la dispozitie un fisier json. Ok, am inteles, e un fisier separat, static si il accesez cu XMLHttpRequest(), JQuery sau fetch si URL-ul cu adresa fisierului. Dar care e cea mai simpla metoda (javascript nativ) de a returna un var ca server cand vine un URL GET request? Sunt incepator in html/javascript si poate vi se pare ciudat ce intreb. ... var foo = 7 var bar = 3 var jsonObjFoo = {"foo":foo} var jsonTxtFoo = JSON.stringify(jsonObj) var jsonObjBar = {"bar":bar} var jsonTxtBar = JSON.stringify(jsonObj) var baseURL = 'https://www.myserver.com/' var fooURL = 'foo' var barURL = 'bar' ceva gen pseudocod: if(requested(https://www.myserver.com/foo)) return foo; ... Se poate accesa direct foo? Sau, am nevoie de jsonObjFoo? Sau,am nevoie de jsonTxtFoo? |
#2
Posted 24 September 2020 - 15:53
Din cate inteleg tu vrei sa trimiti inapoi la client o variabila, un array sau un obiect.
Nu stiu in ce limbaj vrei dar in PHP faci cu echo. echo '{"response": "value"}'; Daca vrei sa faci prin HTTPRequest, clientul primeste raspunsul in variabila responseText (care este de fapt proprietatea obiectului XMLHttpRequest instantiat de tine). Fisier Javascript: //1. Create a new XMLHttpRequest object var XMLHttp = new XMLHttpRequest(); //2. Send request to the server XMLHttp.open("POST", script);//Specifies the type of request & the URL XMLHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");//Set header (only for POST method) stringToSend = JSON.stringify(paramsArray);//Parameter must be string (for GET method, send function has no parameter) XMLHttp.send("JSON=" + stringToSend);//Send function has parameter only for POST method //3. Receive server response function getResponse(){ if (XMLHttp.readyState == 4 && XMLHttp.status == 200) { serverResponse = XMLHttp.responseText;//Get server response as string console.log(serverResponse); serverResponse = JSON.parse(serverResponse);//Convert string to array } } XMLHttp.onreadystatechange = getResponse;//Stores the name of a function to be called automatically each time the readyState property changes Partea de server: <?php $receivedString = $_POST["JSON"];//Receive string from the client $JSONOArray = json_decode($receivedString, true);//JSON to string $arrayToSend = array(); $serverResponse = json_encode($arrayToSend);//Array to string echo $serverResponse;//Send response to the client (In loc de ultimele doua linii poti avea si o singura linie, in care trimiti direct JSON): echo '{"serverResponse": "string2send"}'; ?> Edited by PlatonX, 24 September 2020 - 15:54. |
#3
Posted 24 September 2020 - 17:20
Multumesc, PlantonX! Am mai prins una-alta din ce mi-ai scris. Incerc sa adaptez codul tau la ce am nevoie:
Client: var jsonURL = 'https://www.myserver.com/foo' var request = new XMLHttpRequest() request.open('GET', jsonURL) request.responseType = 'json' request.send() // ce pun la $_POST[]? Nu am un nume pentru stringul trimis Server: var foo = 7 var jsonObjFoo = {"foo":foo} <?php $receivedString = $_POST[]; // ce pun intre [] in loc de "JSON" ca eu am un simplu string fara nume? // In codul tau "JSON" e probabil numele obiectului cu datele primite de la client if($receivedString == 'https://www.myserver.com/foo') echo jsonObjFoo ?> E ok ce am scris? |
#4
Posted 24 September 2020 - 18:28
Daca vrei sa trimiti cu GET si nu cu POST, atunci variabilele sunt trimise prin URL sub forma localhost:port/Path/?variabla1=valoare1&variabla2=valoare2
index.html <!DOCTYPE html> <html> <body> <script src="Client.js"></script> </body> </html> Client.js //=====Server/Client request/response====== //1. Create a new XMLHttpRequest object var XMLHttp = new XMLHttpRequest(); //2. Send request to the server XMLHttp.open("GET", "Server.php?variablia=SentFromClient");//Specifies the type of request & the URL XMLHttp.send(); //3. Receive server response function getResponse(){ if (XMLHttp.readyState == 4 && XMLHttp.status == 200) { serverResponse = XMLHttp.responseText;//Get server response as string serverResponse = JSON.parse(serverResponse);//Convert string to array console.log(serverResponse["response"]); } } XMLHttp.onreadystatechange = getResponse;//Stores the name of a function to be called automatically each time the readyState property changes //=====Server/Client request/response====== Server.php <?php $receivedString = $_GET["variablia"];//Receive string from the client //Send back same string $stringToSend = "Clientul a trimis ".$receivedString; echo '{"response": "'.$stringToSend.'"}';//Send response to the client <--- AICI TRIMITI RASPUNSUL DIRECT CA JSON PENTRU CA AI ACOLADELE ?> Edited by PlatonX, 24 September 2020 - 18:35. |
#5
Posted 24 September 2020 - 19:11
daca ai acces la script, faci o rutina care sa ofere informatiile necesare
|
Anunturi
▶ 0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users