ติดปัญหามรการเขียนส่งข้อมูล method post
Moderator: mindphp, ผู้ดูแลกระดาน
- wkid
- PHP VIP Members
- โพสต์: 2158
- ลงทะเบียนเมื่อ: 17/05/2022 10:37 am
ติดปัญหามรการเขียนส่งข้อมูล method post
อยากทราบตัวอย่างการเขียส่งข้อมูล แบบ method post ระหว่าง javascript ไป python หน่อยครับ
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
- wkid
- PHP VIP Members
- โพสต์: 2158
- ลงทะเบียนเมื่อ: 17/05/2022 10:37 am
Re: ติดปัญหามรการเขียนส่งข้อมูล method post
ส่วน javascript
ส่วน python
คือผมต้องใช้รูแบบรับค่ามาส่วนของ backend ยังไงครับ
โค้ด: เลือกทั้งหมด
const data = { word: word, name: name, check: check };
fetch('http://127.0.0.1:8000/cutword', {
body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
//console.log(data.body);
console.log(data)
nubkumeiei(data, name)
})
.catch((error) => {
console.error('Error:', error);
});
โค้ด: เลือกทั้งหมด
@app.post('/cutword')
def cut():
from pythainlp.tokenize import word_tokenize
import psycopg2 as p2
body = request.get_json(force=True)
IPa = request.remote_addr
q=0
IPaa = ''
print(IPa)
for z in IPa:
if(q==3):
IPaa+='X'
else:
IPaa+=z
if(z=='.'):
q+=1
print(type(IPa))
print(IPaa)
now = datetime.today()
print(now.strftime("%d/%m/%Y %H:%M:%S"))
date = now.strftime("%d/%m/%Y %H:%M:%S")
text = body.get('word')
name = body.get('name')
check = body.get('check')
array1 = []
array2 = []
output = word_tokenize(text, engine="newmm")
countTh = 0
countthai = 0
countE =0
counteng =0
countB =0
print(type(text))
for i in output:
print(i)
countttt = 0
for l in i:
b=l
print(b)
break
if i== " " :
countB +=1
print("ทำงาน")
elif b == "a" or b == "A" or b == "b" or b == "B" or b == "c" or b == "C" or b == "d" or b == "D" or b == "e" or b == "E"or b == "F" or b == "f" or b == "h" or b == "H" or b == "g" or b == "G" or b == "i" or b == "I" or b == "j" or b == "J"or b == "k" or b == "K" or b == "l" or b == "L" or b == "m" or b == "M" or b == "n" or b == "N" or b == "o" or b == "O"or b == "p" or b == "P" or b == "q" or b == "Q" or b == "r" or b == "R" or b == "s" or b == "S" or b == "t" or b == "T"or b == "u" or b == "U" or b == "v" or b == "V" or b == "w" or b == "W" or b == "x" or b == "X" or b == "y" or b == "Y" or b == "z" or b == "Z" :
if i != "\n" :
countttt = len(i)
countE = countttt + countE
counteng = counteng + 1
else :
if i != "\n":
countttt = len(i)
countTh = countttt + countTh
countthai = countthai + 1
array1 = [countB,countTh,countthai,countE,counteng]
for i in array1:
print(i)
if(check==1):
con = p2.connect(
host = host1,
database = db_name,
user = db_user,
password = db_pass,
port = db_port
)
print(con)
cur=con.cursor()
cur.execute('insert into get_data2(username,countb,countth,countthai,counte,counteng,date1,ip,word) values (%s,%s,%s,%s,%s,%s,%s,%s,%s)',(name ,countB,countTh,countthai,countE,counteng,date,IPaa,text))
con.commit()
cur.close()
con.close()
return array1
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
- wkid
- PHP VIP Members
- โพสต์: 2158
- ลงทะเบียนเมื่อ: 17/05/2022 10:37 am
Re: ติดปัญหามรการเขียนส่งข้อมูล method post
error
app.js:39 Error: TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
at getInput (app.js:27:9)
at HTMLInputElement.onclick (index.html:44:93)
app.js:57 Error: TypeError: Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
at app.js:45:13
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
- MBMoo
- PHP VIP Members
- โพสต์: 25879
- ลงทะเบียนเมื่อ: 04/06/2020 10:05 am
Re: ติดปัญหามรการเขียนส่งข้อมูล method post
ตัวอย่าง post ของ fastapi ที่มีการรับค่าค่ะ
โค้ด: เลือกทั้งหมด
@app.post("/items/")
async def create_item(item: Item):
return item
- wkid
- PHP VIP Members
- โพสต์: 2158
- ลงทะเบียนเมื่อ: 17/05/2022 10:37 am
Re: ติดปัญหามรการเขียนส่งข้อมูล method post
ในส่วนของ fetch ถูกต้องไหมพี่
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
- mindphp
- ผู้ดูแลระบบ MindPHP
- โพสต์: 41384
- ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
- ติดต่อ:
Re: ติดปัญหามรการเขียนส่งข้อมูล method post
ตาม Error ของ client
ตัวอย่างการ call api ด้วย post ด้วย fetch
หรือแบบใช้ ajax Jquery
ดูเพิ่มเติม
https://api.jquery.com/jquery.post/
แปลว่า ไม่สามารถ fetch มาได้ เพราะ Request เข้าไปเป็นแบบ get ซึ่ง method นี้ไม่สามารถพวกข้อมูลเข้าไปใน body ได้Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
ตัวอย่างการ call api ด้วย post ด้วย fetch
โค้ด: เลือกทั้งหมด
const params = {
id: 123
}
const response = await fetch('url', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(params)
});
const data = await response.json();
โค้ด: เลือกทั้งหมด
$.ajax({
type: "POST",
url: url,
data: data,
success: success,
dataType: dataType
});
https://api.jquery.com/jquery.post/
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
- wkid
- PHP VIP Members
- โพสต์: 2158
- ลงทะเบียนเมื่อ: 17/05/2022 10:37 am
Re: ติดปัญหามรการเขียนส่งข้อมูล method post
ส่วนของ backend app.post นี่ต้องเขียนรับค่ายังไงครับ ช่วยอธิบายเพิ่มเติมทีครับmindphp เขียน: ↑17/06/2022 2:48 pm ตาม Error ของ clientแปลว่า ไม่สามารถ fetch มาได้ เพราะ Request เข้าไปเป็นแบบ get ซึ่ง method นี้ไม่สามารถพวกข้อมูลเข้าไปใน body ได้Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
ตัวอย่างการ call api ด้วย post ด้วย fetchหรือแบบใช้ ajax Jqueryโค้ด: เลือกทั้งหมด
const params = { id: 123 } const response = await fetch('url', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params) }); const data = await response.json();
ดูเพิ่มเติมโค้ด: เลือกทั้งหมด
$.ajax({ type: "POST", url: url, data: data, success: success, dataType: dataType });
https://api.jquery.com/jquery.post/
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
- mindphp
- ผู้ดูแลระบบ MindPHP
- โพสต์: 41384
- ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
- ติดต่อ:
Re: ติดปัญหามรการเขียนส่งข้อมูล method post
มีคนแนะนำไว้ให้แล้วbolue เขียน: ↑17/06/2022 2:21 pm ตัวอย่าง post ของ fastapi ที่มีการรับค่าค่ะ
โค้ด: เลือกทั้งหมด
@app.post("/items/") async def create_item(item: Item): return item
เพิ่มเติม fastapi
https://www.mindphp.com/%E0%B8%9A%E0%B8 ... stapi.html
ติดตาม VDO: http://www.youtube.com/c/MindphpVideoman
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
ติดตาม FB: https://www.facebook.com/pages/MindphpC ... 9517401606
หมวดแชร์ความรู้: https://www.mindphp.com/forums/viewforum.php?f=29
รับอบรม และพัฒนาระบบ: https://www.mindphp.com/forums/viewtopic.php?f=6&t=2042
- wkid
- PHP VIP Members
- โพสต์: 2158
- ลงทะเบียนเมื่อ: 17/05/2022 10:37 am
Re: ติดปัญหามรการเขียนส่งข้อมูล method post
ลองแล้วขึ้นแบบนี้ครับ
โหลดส่วนขยายแล้วเปิดใช้ละนะครับindex.html:1 Access to fetch at 'http://127.0.0.1:8000/cutword' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.
app.js:28 POST http://127.0.0.1:8000/cutword net::ERR_FAILED
getInput @ app.js:28
onclick @ index.html:44
app.js:28 Uncaught (in promise) TypeError: Failed to fetch
at getInput (app.js:28:34)
at HTMLInputElement.onclick (index.html:44:93)
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
- wkid
- PHP VIP Members
- โพสต์: 2158
- ลงทะเบียนเมื่อ: 17/05/2022 10:37 am
Re: ติดปัญหามรการเขียนส่งข้อมูล method post
แก้ไชได้ละครับ คือไปเปิดตัวคอลทั้งหมดใน python เพื่อส่งไม่ให้ติดคอลครับ
โค้ด: เลือกทั้งหมด
origins = [
"*"
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
-
- Similar Topics
- ตอบกลับ
- แสดง
- โพสต์ล่าสุด
-
-
โพสต์ใหม่ สรุปการใช้ การใช้งาน POST - Method สร้าง API ให้เรียกผ่านการ Post
โดย fonfonn » 22/07/2022 7:00 pm » ใน Python Knowledge - 1 ตอบกลับ
- 768 แสดง
-
โพสต์ล่าสุด โดย บุคคลทั่วไป
23/07/2022 2:01 pm
-
-
-
โพสต์ใหม่ การเขียน fast api โดยเรียกใช้ method post
โดย wkid » 18/06/2022 12:50 pm » ใน Python Knowledge - 0 ตอบกลับ
- 772 แสดง
-
โพสต์ล่าสุด โดย wkid
18/06/2022 12:50 pm
-
-
-
โพสต์ใหม่ แก้ปัญหา Plugin WP Post Rating แชร์ Post แล้วมี %20 ขึ้นมาแทนช่อง
โดย mindphp » 29/10/2022 8:21 pm » ใน Wordpress Developing Knowledge - 0 ตอบกลับ
- 1090 แสดง
-
โพสต์ล่าสุด โดย mindphp
29/10/2022 8:21 pm
-
-
-
โพสต์ใหม่ Deposit money into account with Thailand Post by Bank@post service
โดย numtan5839 » 18/11/2019 11:42 am » ใน English Language - 0 ตอบกลับ
- 1774 แสดง
-
โพสต์ล่าสุด โดย numtan5839
18/11/2019 11:42 am
-
ผู้ใช้งานขณะนี้
สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 104