简体   繁体   中英

Http response returning two string value in Python

Hi I have two string in Python like

string1 = "['Rilassante piacere', 'Abbraccio vellutato', 'Dosha Vata']";
string2 = "[1, 1, 4]";

I have tried passing in the response

return func.HttpResponse(string1,string2, status_code=200)

But obiviusly it is not possible, should I encode them in JSON?

So my questions are:

  • How I can send this two string in HttpResponse in my Python script?

  • From the calling the URL 'https://example.com/response' how I can use this response in JS to retrieve these strings?

Thanks in advance

You can use the below code and see if it helps

return HttpResponse(json.dumps({"string1":string1, "string2":string1}), status_code=200)

you can add as many variables or data as you want inside the JSON object.

to make a request for pure javascript try the below solution

const Http = new XMLHttpRequest();
const url='https://jsonplaceholder.typicode.com/posts'; this url is the url of your python endpoint
Http.open("GET", url);
Http.setRequestHeader('Access-Control-Allow-Headers', '*');
Http.send();
console.log(Http.response)
response = Http.response // this response should be an object when you use your url
status1 = response['status1']
status2 = response['status2']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM