iT邦幫忙

1

[PHP] AJAX請求無法得到回應問題

各位前輩好!我是剛開始學習前端語言的小菜鳥。目前正在學習使用PHP以及AJAX寫出留言板的登入介面。

我使用 sign_in_handle.php 這個檔案向後端取得資料,比對輸入的帳號密碼。若正確就返回OK,錯誤則返回 ERROR。另外也使用了 sign_in.js 這個檔案 用 AJAX 向 sign_in_handle.php 取得回應。

sign_in.html
https://ithelp.ithome.com.tw/upload/images/20200315/2012561479uUv0RGJE.png

sign_in.js
https://ithelp.ithome.com.tw/upload/images/20200315/20125614w2ZY5DKJQK.png

sign_in_handle.php
https://ithelp.ithome.com.tw/upload/images/20200315/20125614ou5blla2ut.png

但很奇怪的是...sign_in.js 這個檔案好像無論如何都只會接收到 ERROR 的回應。儘管已經輸入了正確的帳號密碼。

頁面會出現 alert("ERROR") ,但 sign_in_handle.php 卻是給出了 OK 的回應。
https://ithelp.ithome.com.tw/upload/images/20200315/20125614AXLBGlK3yB.png
https://ithelp.ithome.com.tw/upload/images/20200315/201256147cLQDtHhzx.png

好像下面這個部分沒有作用...但卻還是會幫我 verify 帳號密碼然後返回OK。
https://ithelp.ithome.com.tw/upload/images/20200315/20125614EFdf4bSCKN.png
https://ithelp.ithome.com.tw/upload/images/20200315/20125614Ukh0dmTfoB.png
https://ithelp.ithome.com.tw/upload/images/20200315/20125614slJnifwvfs.png
我已經卡關三天了...檢查了好幾次,但還是不知道到底哪個環節出錯...
拜託前輩們如果稍微點一下我,先感謝大家花時間了...
https://ithelp.ithome.com.tw/upload/images/20200318/20125614How6OiBxA9.png
https://ithelp.ithome.com.tw/upload/images/20200318/20125614FD4GOiQc9P.png
https://ithelp.ithome.com.tw/upload/images/20200318/20125614mtrFPPn6a4.png

看更多先前的討論...收起先前的討論...
dragonH iT邦超人 5 級 ‧ 2020-03-15 15:49:38 檢舉
js 那 把 res 印出來看
我有試過,確實是收到 ERROR 的 response 沒錯。只是不管怎樣卻都收不到 OK,php 卻還是可以echo 出 {"success": "OK"},不知道為什麼...
Han iT邦研究生 1 級 ‧ 2020-03-15 16:15:53 檢舉
從F12的紀錄 顯示正常嗎?
沒有任何異常狀態...
Han iT邦研究生 1 級 ‧ 2020-03-15 16:23:52 檢舉
意思是 f12顯示 success:ok 但是alert還是出現error?
沒錯!!!
是我的程式碼有哪邊寫錯嗎??
Han iT邦研究生 1 級 ‧ 2020-03-15 17:06:36 檢舉
收到的res是string 還是 object
照理說有加dataType:json基本上會是object 不過還是確保一下好了
dragonH iT邦超人 5 級 ‧ 2020-03-15 17:07:36 檢舉
可以補一下 你 console

network 那頁

看一下 php 回傳給你什麼嗎

就算是 object 的問題

我覺得應該也不會 alert('ERROR')
@Han 應該是object
@dragonH 我補上network那頁,但不知道是不是你說的內容,因為我不太會看
dragonH iT邦超人 5 級 ‧ 2020-03-15 17:22:46 檢舉
在 ajax 那

console.log(typeof res)

看看
dragonH iT邦超人 5 級 ‧ 2020-03-15 17:42:57 檢舉
另外想問一下

為啥你 call

sign_in_handle.php

用的是 GET
是object 沒錯。
我以為跟 sign_in_handle.php 取資料,不用給資料,所以是用GET。難道是我這邊錯了??
dragonH iT邦超人 5 級 ‧ 2020-03-16 09:25:04 檢舉
可是你的 sign_in_handle.php 看起來適用 post 拿資料餒

你 po 的 console network 截圖

request methds 也是 post

所以我才覺得奇怪
Season iT邦研究生 4 級 ‧ 2020-03-16 09:42:44 檢舉
最近也在學寫網頁
也想問下
js沒有阻止form表單送出
這樣不就等於送出2個請求??
個人認為你的success成功是針對表單的
js 失敗是因
你php用$_POST[]取參數
js method:"get" 應改 "post"
然後我在學的時候是都有
data: {username:username, user_password:user_password}
沒有寫應該不會把帳密傳到後端吧
有錯還請指教
小魚 iT邦大師 1 級 ‧ 2020-03-16 13:43:56 檢舉
的確有可能,
ajax跟form選擇一種就好.
感謝前輩們的指點~我好像知道我的問題點在哪了
不好意思... 這邊問個蠢問題。用$.ajax 該如何 使用method: POST 讓 PHP 的 $_POST[] 接收到資料呢??
Franky Chen iT邦研究生 3 級 ‧ 2020-03-17 13:59:26 檢舉
> 不好意思... 這邊問個蠢問題。用$.ajax 該如何 使用method: POST 讓 PHP 的 $_POST[] 接收到資料呢??

表單直接用js抓輸入欄位的資料


data: {
欄位:值
},

post到後端用 $_POST 接就好
$(function () {
$('.sign_in_btn').on('click', ()=>{
let currentUsername = $('[name=username]').val();
let currentPassword = $('[name=user_password]').val();

alert(currentUsername+currentPassword);

if (checkInfo (currentUsername) && checkInfo (currentPassword)) {

$.ajax({
method: "POST",
url: "sign_in_handle.php",
data: {"currentUsername": currentUsername, "currentPassword": currentPassword},
success: (res) => {
if (res.success === 'OK') {
alert('OK');
} else if (res.success === 'ERROR') {
alert('ERROR');
}
}
});
} else if (!checkInfo (currentUsername) || !checkInfo (currentPassword)) {
$(".login_warning").text("Incorrect username or password.").css("visibility", "visible");
}
})
})


function checkInfo (detail) {
if (!detail) {
return false;
} else {
return true;
}
}

我寫出了這樣...但php完全沒有反應
Season iT邦研究生 4 級 ‧ 2020-03-18 09:29:28 檢舉
你PHP 是用$_POST["username"]還是用$_POST["currentUsername"]來取值的?
因為你js是用"currentUsername" 所以 php要用$_POST["currentUsername"]來取值
我是用$_POST["currentUsername"]取值的,但還是取不到值,好像ajax端 東西傳不過去的樣子...我將我的程式碼重新上傳到主題了...不知道有沒有哪位前輩願意替我看看...感激不盡了
我終於找到原因啦!!!就像這篇說的https://blog.csdn.net/u010855333/article/details/60766096
一切都是form標籤的問題...
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
咖咖拉
iT邦好手 1 級 ‧ 2020-03-16 17:57:50

建議你把success 寫個編號
success1
success2
...
你這樣錯誤訊息都一樣
你要怎麼分辨是誰的問題/images/emoticon/emoticon06.gif

了解了!謝謝前輩的指教

我要發表回答

立即登入回答