dongyi6269 2013-08-31 21:36
浏览 126

JSON登录中的AJAX POST请求

I'm trying to create a very simple login form using AJAX for server comunication and PHP as a server-side script. As a starting point I was just trying to send the login data to the server via JSON, but it already gave me issues.

Problem is, when I send the data with a POST request in AJAX my server doesn't seem to get anything and returns no data at all. Here's the HTML for the request:

<!DOCTYPE HTML>
<html>
<head>
<title> Login experiment </title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>

$(document).ready(function AjaxJSON() {
    $("form#loginForm").submit(function() {
        var username = $('#username').val();
        var password = $('#password').val();
        var jsonlogin = "{\"username\" : \"" + username + "\" , \"password\" : \"" + password + "\"}";
        var str = 'json=' + jsonlogin;
        $.ajax({
            type: 'POST',
            url: 'login.php',
            // URL of my PHP script
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            data: str,
            success: function(data) {
                alert("Data received!" + data);
            }
        });
        // ajax
        })
    })

</script>
</head>

<body>
<h1> LOGIN: </h1>
<form id="loginForm" name="loginForm" method="post" action="">
<input type="text" id="username" name="username"> USERNAME <br>
<input type="password" id="password" name="password"> PASSWORD <br>
<input type="submit" class="button positive">
</form>
</body>
</html>

This HTML code should simply send the request form (username+password) coded in a JSON string to the PHP script. The script is something like this:

<?php
$result = json_decode($_POST['json']);
echo $result->username;
echo " ";
echo $result->password;
?>

Why doesn't it show me any reply? Thank you in advance for your help :)

  • 写回答

1条回答 默认 最新

  • dqy13020940 2013-08-31 21:43
    关注

    Your main problem is that you are claiming that you are sending application/json; charset=utf-8. This is an issue for two reasons:

    1. PHP doesn't have any build in methods for handling JSON encoded POST data so $_POST won't be populated
    2. Your data isn't JSON. It is (improperly escaped) application/x-www-form-urlencoded data (which includes a string of (improperly escaped) JSON as one of its pieces of data)

    If you want to send JSON, then:

    • Build it using JSON.stringfy not by mashing together strings
    • Don't stick json= on the front of it
    • Get the data out of the POST body as per this question

    If you aren't tied to JSON, then just use the standard methods for encoding form data:

    • Forget the jsonlogin line
    • Don't override the default contentType
    • Pass an object of your data to the data argument: data: { username: username, password: password }

    Your next problem is that you are saying dataType: 'json' but your PHP is returning (invalid) HTML (that PHP is vulnerable to XSS attacks too).

    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?