简体   繁体   English

$.ajax( type: “POST” POST 方法到 php

[英]$.ajax( type: “POST” POST method to php

I'm trying to use the POST method in jQuery to make a data request.我正在尝试使用 jQuery 中的 POST 方法来发出数据请求。 So this is the code in the html page:所以这是html页面中的代码:

<form>
Title : <input type="text" size="40" name="title"/>
<input type="button" onclick="headingSearch(this.form)" value="Submit"/><br /><br />
</form>
<script type="text/javascript">
function headingSearch(f)
{
    var title=f.title.value;
    $.ajax({
      type: "POST",
      url: "edit.php",
      data: {title:title} ,
      success: function(data) {
        $('.center').html(data); 
      }
    });
}
</script>

And this is the php code on the server :这是服务器上的 php 代码:

<?php

$title = $_POST['title'];
if($title != "")
{
    echo $title;
}

?>

The POST request is not made at all and I have no idea why.根本没有发出 POST 请求,我不知道为什么。 The files are in the same folder in the wamp www folder so at least the url isn't wrong.这些文件位于 wamp www 文件夹中的同一文件夹中,因此至少 url 没有错误。

You need to use data: {title: title} to POST it correctly.您需要使用data: {title: title}正确发布它。

In the PHP code you need to echo the value instead of return ing it.在 PHP 代码中,您需要echo显值而不是return值。

Check whether title has any value or not.检查标题是否有任何价值。 If not, then retrive the value using Id.如果没有,则使用 Id 检索该值。

<form>
Title : <input type="text" id="title" size="40" name="title" value = ''/>
<input type="button" onclick="headingSearch(this.form)" value="Submit"/><br /><br />
</form>
<script type="text/javascript">
function headingSearch(f)
{
    var title=jQuery('#title').val();
    $.ajax({
      type: "POST",
      url: "edit.php",
      data: {title:title} ,
      success: function(data) {
    $('.center').html(data); 
}
});
}
</script>

Try this code.试试这个代码。

In php code, use echo instead of return .在 php 代码中,使用echo而不是return Only then, javascript data will have its value.只有这样,javascript 数据才会有价值。

try this试试这个

$(document).on("submit", "#form-data", function(e){
    e.preventDefault()
    $.ajax({
        url: "edit.php",
        method: "POST",
        data: new FormData(this),
        contentType: false,
        processData: false,
        success: function(data){
            $('.center').html(data); 
        }
    })
})

in the form the button needs to be type="submit"在表单中,按钮需要是type="submit"

Id advice you to use a bit simplier method -我建议您使用更简单的方法-

$.post('edit.php', {title: $('input[name="title"]').val() }, function(resp){
    alert(resp);
});

try this one, I just feels its syntax is simplier than the $.ajax's one...试试这个,我只是觉得它的语法比 $.ajax 的语法更简单......

function signIn()
{
    var Username = document.getElementById("Username").value;
    var Password = document.getElementById("Password").value;

    $.ajax({
        type: 'POST',
        url: "auth_loginCode.jsp",
        data: {Username: Username, Password: Password},
        success: function (data) {
            alert(data.trim());
            window.location.reload();
        }
    });
}
contentType: 'application/x-www-form-urlencoded'

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

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