ติดปัญหามรการเขียนส่งข้อมูล method post

แนะนำ สอบถาม ภาษา C สำหรับผู้เริ่มต้น ภาษา Java ภาษา Python

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
wkid
PHP VIP Members
PHP VIP Members
โพสต์: 2158
ลงทะเบียนเมื่อ: 17/05/2022 10:37 am

ติดปัญหามรการเขียนส่งข้อมูล method post

โพสต์ที่ยังไม่ได้อ่าน โดย wkid »

อยากทราบตัวอย่างการเขียส่งข้อมูล แบบ method post ระหว่าง javascript ไป python หน่อยครับ
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
ภาพประจำตัวสมาชิก
wkid
PHP VIP Members
PHP VIP Members
โพสต์: 2158
ลงทะเบียนเมื่อ: 17/05/2022 10:37 am

Re: ติดปัญหามรการเขียนส่งข้อมูล method post

โพสต์ที่ยังไม่ได้อ่าน โดย wkid »

ส่วน javascript

โค้ด: เลือกทั้งหมด

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);
            });
ส่วน python

โค้ด: เลือกทั้งหมด

@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
คือผมต้องใช้รูแบบรับค่ามาส่วนของ backend ยังไงครับ
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
ภาพประจำตัวสมาชิก
wkid
PHP VIP Members
PHP VIP Members
โพสต์: 2158
ลงทะเบียนเมื่อ: 17/05/2022 10:37 am

Re: ติดปัญหามรการเขียนส่งข้อมูล method post

โพสต์ที่ยังไม่ได้อ่าน โดย wkid »

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
PHP VIP Members
โพสต์: 25602
ลงทะเบียนเมื่อ: 04/06/2020 10:05 am

Re: ติดปัญหามรการเขียนส่งข้อมูล method post

โพสต์ที่ยังไม่ได้อ่าน โดย MBMoo »

ตัวอย่าง post ของ fastapi ที่มีการรับค่าค่ะ

โค้ด: เลือกทั้งหมด

@app.post("/items/")
async def create_item(item: Item):
    return item
ภาพประจำตัวสมาชิก
wkid
PHP VIP Members
PHP VIP Members
โพสต์: 2158
ลงทะเบียนเมื่อ: 17/05/2022 10:37 am

Re: ติดปัญหามรการเขียนส่งข้อมูล method post

โพสต์ที่ยังไม่ได้อ่าน โดย wkid »

ในส่วนของ fetch ถูกต้องไหมพี่
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ติดปัญหามรการเขียนส่งข้อมูล method post

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

ตาม Error ของ client
Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
แปลว่า ไม่สามารถ fetch มาได้ เพราะ Request เข้าไปเป็นแบบ get ซึ่ง method นี้ไม่สามารถพวกข้อมูลเข้าไปใน 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 Jquery

โค้ด: เลือกทั้งหมด

$.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
ภาพประจำตัวสมาชิก
wkid
PHP VIP Members
PHP VIP Members
โพสต์: 2158
ลงทะเบียนเมื่อ: 17/05/2022 10:37 am

Re: ติดปัญหามรการเขียนส่งข้อมูล method post

โพสต์ที่ยังไม่ได้อ่าน โดย wkid »

mindphp เขียน: 17/06/2022 2:48 pm ตาม Error ของ client
Failed to execute 'fetch' on 'Window': Request with GET/HEAD method cannot have body.
แปลว่า ไม่สามารถ fetch มาได้ เพราะ Request เข้าไปเป็นแบบ get ซึ่ง method นี้ไม่สามารถพวกข้อมูลเข้าไปใน 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 Jquery

โค้ด: เลือกทั้งหมด

$.ajax({
  type: "POST",
  url: url,
  data: data,
  success: success,
  dataType: dataType
});
ดูเพิ่มเติม
https://api.jquery.com/jquery.post/
ส่วนของ backend app.post นี่ต้องเขียนรับค่ายังไงครับ ช่วยอธิบายเพิ่มเติมทีครับ
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
ภาพประจำตัวสมาชิก
mindphp
ผู้ดูแลระบบ MindPHP
ผู้ดูแลระบบ MindPHP
โพสต์: 41232
ลงทะเบียนเมื่อ: 22/09/2008 6:18 pm
ติดต่อ:

Re: ติดปัญหามรการเขียนส่งข้อมูล method post

โพสต์ที่ยังไม่ได้อ่าน โดย mindphp »

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
ภาพประจำตัวสมาชิก
wkid
PHP VIP Members
PHP VIP Members
โพสต์: 2158
ลงทะเบียนเมื่อ: 17/05/2022 10:37 am

Re: ติดปัญหามรการเขียนส่งข้อมูล method post

โพสต์ที่ยังไม่ได้อ่าน โดย wkid »

ลองแล้วขึ้นแบบนี้ครับ
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
PHP VIP Members
โพสต์: 2158
ลงทะเบียนเมื่อ: 17/05/2022 10:37 am

Re: ติดปัญหามรการเขียนส่งข้อมูล method post

โพสต์ที่ยังไม่ได้อ่าน โดย wkid »

แก้ไชได้ละครับ คือไปเปิดตัวคอลทั้งหมดใน python เพื่อส่งไม่ให้ติดคอลครับ

โค้ด: เลือกทั้งหมด

origins = [
    "*"
]

app.add_middleware(
        CORSMiddleware,
        allow_origins=origins,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
)
ทำไมสัตว์ที่น่ากลัวที่สุดถึงตัวเล็กๆที่เรียกว่า Bug ละนั่น );
ตอบกลับโพส
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: Google [Bot], Google Adsense [Bot] และบุคลทั่วไป 88