为什么在URI中无法识别用户名

时间:2013-12-23 13:50:08

标签: javascript php ajax web-services

我正在尝试通过URI传递用户名,但在控制台中,它表示未指定URI,可以在此link上测试代码。

AJAX

function Putaccount() { //an account for the user
    $.ajax({
        type: 'put',
        url: 'http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/user/adduser/' + $("#username").val(),
        contentType: "text",
        data: {
            name: $("#name").val(),
            password: $("#password").val(),
            email: $("#email").val(),
            telephone: $("#telephone").val(),

            //pass the users details to the webservice
        },
        dataType: 'json',
        success: function (data) {
            alert("Account has been created. Log in using your username and password");
            $.mobile.changePage("#login"); //show the login page

        },
        error: function (response) {
            var r = jQuery.parseJSON(response.responseText);

            alert("Error:" + r.error.text);
        }
    });
}

PHP

function adduser_put($username = null) {

    // checking for valid parameters...
    {
        if (!isset($username)) {
            $info - > status = 'failure';
            $info - > error - > code = 36;
            $info - > error - > text = 'requested username not specified in URI';
            $this - > response($info, 400);
        }
    }
    $password = $this - > put('password');
    $telephone = $this - > put('telephone');
    $email = $this - > put('email');
    $name = $this - > put('name');
    //$dob = $this->put('date');
    // if the username and password parameters missing
    if (empty($password) || empty($email) || empty($telephone) || empty($name)) {
        $info - > status = 'failure';
        $info - > error - > code = 13;
        $info - > error - > text = '* All field are required';
        $this - > response($info, 400);
    }

    //check if the username already exists in the user table
    {
        $this - > load - > database();
        $sql = 'SELECT COUNT(userid) AS records FROM users WHERE username = "'.$username.
        '";';
        $query = $this - > db - > query($sql);
        $data = $query - > row();
        if ($data - > records == "1") {
            $info - > status = 'failure';
            $info - > error - > code = 14;
            $info - > error - > text = 'username already exists';
            $this - > response($info, 400);
        }
    }


    unset($data);
    $password = md5($password);
    $password = $this - > put('password');
    $data - > name = $this - > put('name');
    $data - > telephone = $this - > put('telephone');
    $data - > email = $this - > put('email');
    //$data->date = $this->put('date');
    $data - > username = $username;
    $authstring = $username.
    ':'.$password;
    $data - > authorization = base64_encode($authstring);
    $this - > db - > insert('users', $data);
    $info - > status = 'created';
    $info - > selfLink = 'http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/user/adduser/'.$username;
    unset($data - > authorization);
    $data - > password = $password;
    $info - > user = $data;
    $json = json_encode($info);
    $this - > response($info, 200);

}

HTML

<form id="register" class="ui-body ui-body-a ui-corner-all">
    <fieldset>
        <div data-role="fieldcontain">
            <label for="name">Name:</label>
            <input type="text" value="" name="name" id="name" />
        </div>
        <div data-role="fieldcontain">
            <label for="email">Email:</label>
            <input type="text" value="" name="email" id="email" />
        </div>
        <div data-role="fieldcontain">
            <label for="telephone">Telephone:</label>
            <input type="text" value="" name="telephone" id="telephone" />
        </div>
        <div data-role="fieldcontain">
            <label for="username">Username:</label>
            <input type="text" value="" name="username" id="username" />
        </div>
        <div data-role="fieldcontain">
            <label for="password">Password:</label>
            <input type="password" value="" name="password" id="password" />
        </div>
        <div>
            <input type="button" data-theme="a" name="submit" onclick="Putaccount()" value="Submit">
    </fieldset>
</form>

1 个答案:

答案 0 :(得分:0)

这里有两件事,当我在页面上尝试$(“#username”)返回一个textarea而不是表单中的输入时,在重命名表单输入的id之后我看到输入有值=“”而且它编辑后没有更改,所以从输入中删除该标记值并将id更改为username2我得到正确的值。

尝试将您的html代码更改为:

<div data-role="fieldcontain">
    <label for="username">Username:</label>
    <input type="text" value="" name="username" id="username2" />
</div>

和您的Javascript到此

.ajax({
        type: 'put',
        url: 'http://creative.coventry.ac.uk/~4078078/moviereviews/v1.0/index.php/user/adduser/' + $("#username2").val()
....