从ajax调用PHP脚本获取JSON响应

时间:2021-07-24 01:24:08

I have been successful in returning the json responce from the ajax call to my php script that sends messages. When submitting data to the PHP script, if successful it should return a json string like the following...

我成功地将json响应从ajax调用返回到php脚本,该脚本发送消息。当向PHP脚本提交数据时,如果成功,它应该返回如下所示的json字符串……

{"status":"valid","message":"Your message was send successfully."}

I would like to check if the "status" equals valid and then show a div showing that the message has been sent. How would i go about doing this and this is the code i have so far...

我想检查“状态”是否为有效,然后显示一个div,显示消息已经发送。我该怎么做呢?这就是我到目前为止的代码。

$("#send").click(function() {
        var complete = true;
        $('input#fullname, input#email, input#subject, textarea#message').each(function() {
            if ($(this).val()) {
                $(this).css("background","#121212").css("color","#5c5c5c");
            } else {
                $(this).css("background","#d02624").css("color","#121212");
                complete = false;
            }
        });
        if (complete == true){
            var name = $("input#fullname").val();
            var email = $("input#email").val();
            var subject = $("input#subject").val();
            var message = $("textarea#message").val();
            var data = 'name='+name+'&email='+email+'&subject='+subject+'&message='+message;
            $.ajax({
                type:"POST",  
                url:"resources/includes/contact.php",  
                data:data,
                success:function(data){
                    alert(data);
                }
            });
        }
    });

Thanks,
Nathaniel Blackburn

谢谢,纳撒尼尔·布莱克本

3 个解决方案

#1


0  

In your success callback function, add

在成功回调函数中,添加

data = $.parseJSON(data);
if (data.status == "valid"){
     //do stuff
}

#2


1  

you can do it like this

你可以这样做。

$.ajax({
                type:"POST",  
                url:"resources/includes/contact.php",  
                data:data,
                dataType: 'json',
                success:function(response){
                   if(response.status="valid"){
                        ......
                  }

                }
            });

#3


0  

change your success function to look like this:

改变你的成功函数如下:

success:function(data)
{
    jsonObject = JSON.parse(data);
    alert(jsonObject.status);
}

#1


0  

In your success callback function, add

在成功回调函数中,添加

data = $.parseJSON(data);
if (data.status == "valid"){
     //do stuff
}

#2


1  

you can do it like this

你可以这样做。

$.ajax({
                type:"POST",  
                url:"resources/includes/contact.php",  
                data:data,
                dataType: 'json',
                success:function(response){
                   if(response.status="valid"){
                        ......
                  }

                }
            });

#3


0  

change your success function to look like this:

改变你的成功函数如下:

success:function(data)
{
    jsonObject = JSON.parse(data);
    alert(jsonObject.status);
}