如何使用/组合使用两种不同数据类型的2个jQuery脚本 [英] How to use / combine 2 jquery scripts that use two different datatypes

查看:73
本文介绍了如何使用/组合使用两种不同数据类型的2个jQuery脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tomas一直在帮助我解决这个问题,我认为我们已经取得了一些实际进展.这是修改后的代码.这应该做的是首先获取我的查询字符串,并将其传递到服务器,在该服务器中解析并在数据库中插入数据.当类方法返回成功时,JQ函数应使用来自服务器的消息回显100%css样式的div(id = message).它还可以返回失败消息.此消息已导入???与json.我可以在FF控制台中看到该消息,但在浏览器中看不到该消息.

Tomas has been helping me on this problem and we have made some real progress I think. Here is the modified code. What this is supposed to do is to first, take my query string and pass it to the server where the data is parsed and inseted in the database. When the class method returns a success, the JQ function should echo a 100% css styled div (id=message) with the message from the server. It can also return a failure message as well. This message is imported?? with json. I can see the message in the FF console but I just can't see the message in the browser.

我遇到的另一个问题是页面上的每个div都附加了id = message.

Another issue I am having is that every div on the page is having the id=message appended to it.

$.ajax({
    type: "POST",
    url: "body.php?action=admCust",
    data: dataString,
    success: function(data){
        $('#admCust input[type=text]').val('');
        var div = $('<div>').attr('id', 'message').html(data.message);
        if(data.success == 0) {
            $(div).addClass('error');
        } else {
            $(div).addClass('success');
        }
        $('body').append(div);
        $(div).show();
      }
});
return false;

推荐答案

尝试下面的代码(因为它的末尾有return false;,所以我认为这是在commit函数之内或正在填充dataString从其他地方开始...):

Try this code (since it has a return false; at the end I'm assuming this is inside a submit function or something and dataString is being populated from elsewhere....):

$.ajax({
    type: "POST",
    url: "body.php?action=admCust",
    data: dataString,
    dataType: 'json',
    success: function(data){
        $('#admCust input[type=text]').val('');
        var div = $('<div>').attr('id', 'message').html(data.message);
        if(data.success == 0) {
            $(div).addClass('error');
        } else {
            $(div).addClass('success');
        }
        $('body').append(div);
      }
});
return false;

您最大的错误是您忘记指定dataType(在我的示例中为...),因此jQuery不知道服务器将返回哪种格式.它只是假设它是一个字符串而不是一个JSON对象,所以您的data变量未正确填充.您也不需要show(),因为追加就足够了.

Your biggest mistake was that you forgot to specify a dataType (which I had in my example...) so jQuery did not know what format the server would be returning. It simply assumed it was a string instead of a JSON object so your data variable wasn't populated correctly. You also don't need the show() as appending is enough.

我对上面的内容进行了测试,并假设dataString填充在其他位置,并且body.php返回带有消息和成功代码的有效JSON字符串.

I tested the above and it works, assuming, again, dataString is populated elsewhere and body.php is returning a valid JSON string with a message and a success code.

这篇关于如何使用/组合使用两种不同数据类型的2个jQuery脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆