简体   繁体   English

PHP中的Jquery Ajax数据发布问题

[英]Jquery Ajax Data Post Problem in PHP

I am using jquery's AJAX in my project. 我在我的项目中使用jquery的AJAX。 Today, I used it somewhere else with all same themethods but it doesn't work. 今天,我在其他地方使用了所有相同的方法,但它不起作用。

Is there something wrong with my script? 我的脚本有问题吗?

HTML: HTML:

<a class='btn edit_receipe_btn' id='myreceipe-52'>Edit</a>

JQuery: JQuery的:

(Click function works. When I put alert(instance) after var instance line, it works) (点击功能有效。当我在var instance行之后放置alert(instance) ,它可以工作)

$(document).ready(function(){
$('.edit_receipe_btn').click(function(){
   var instance = $(this).attr('id');
   var dataString = 'process=userReceipeEdit&instance='+instance;
   $.ajax({
    type: 'POST',
    url: 'ajax/ajaxs.php',
    data: dataString,
    cache: false,
    success: function(msg) {
        alert(msg);
    }
    });
});
});

PHP: PHP:

$prcs = $_POST['process'];
if($prcs=='userReceipeEdit'){
        $instance = $_POST['instance'];
        return $instance;
    }

It appears the problem is in the PHP. 看来问题出在PHP中。 What am I doing wrong? 我究竟做错了什么?

Is that the entire PHP page? 那是整个PHP页面吗? if so, you should echo instead of return 如果是这样,你应该回声而不是回归

As Jasper De Bruijn notified me, the problem was in my php script. 正如Jasper De Bruijn通知我的那样,问题出在我的PHP脚本中。 I should use echo instead of return: Wrong usage: 我应该使用echo而不是return:错误的用法:

$prcs = $_POST['process'];
if($prcs=='userReceipeEdit'){
        $instance = $_POST['instance'];
        return $instance;
    }

Correct usage: 正确用法:

$prcs = $_POST['process'];
if($prcs=='userReceipeEdit'){
        $instance = $_POST['instance'];
        echo $instance;
    }

Two things, output the error and check to make sure instance is not undefined. 两件事,输出错误并检查以确保实例未定义。

Output the error like this: 像这样输出错误:

$('.edit_receipe_btn').click(function(){
   var instance = $(this).attr('id');
   var dataString = 'process=userReceipeEdit&instance='+instance;
   $.ajax({
    type: 'POST',
    url: 'ajax/ajaxs.php',
    data: dataString,
    cache: false,
    success: function(msg) {
        alert(msg);
    },
    error: function(response, status, error)
    { 
        alert(response.responseText);
        alert(status);
        alert(error);
    }
    });
});

I think you have formed this POST like a get, not sure why. 我认为你已经把这个POST形成了一个get,不知道为什么。 Try doing this in JS: 尝试在JS中执行此操作:

var dataString = 
    {
        "process" : "userReceipeEdit",
        "instance" : instance
    };

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

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