带特殊字符的JQuery AJAX POST请求 [英] JQuery AJAX POST request with Special Characters

查看:92
本文介绍了带特殊字符的JQuery AJAX POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用JQuery AJAX发送数据的管理页面存在问题,但是它没有处理&或,原因很明显(它们打破了数据字符串/ url)。

I have an issue with an admin page that sends data using JQuery AJAX, but it doesn't handle & or ", and the reason is obvious (they break the data string/url).

使用允许这些字符的Javascript发送此数据的正确方法是什么?使用一组数据可以解决&但它会解决?

What is the correct way to send this data using Javascript that will allow these characters? Using an array of data may solve for & but would it solve for "?

必须接受的示例输入:执行此操作&按输入

Example input that must be acceptable: Do this & press "enter"

function SaveCombinations() {
    var priority = 0;
    $("#sort li").each(function() {
        $.ajax({"url": "lib/sc-save.php", "data": "id="+this.id+"&name="+$(this).find("input").val()+"&priority="+priority, "type": "POST",
        "success": function() {}});
        priority++;
    });
}


推荐答案

将数据作为对象传递,它会照顾你:

Pass your data as an object, and it will be taken care of for you:

function SaveCombinations() {
    var p = 0;

    $('#sort li').each(function(){
        var $id = this.id,
            $name = $(this).find('input').val(),
            $priority = p, // you don't really need to do this, its just a maintainability thing in case it changes in the future
            $data = {id:$id,name:$name,priority:$priority};

        $.ajax({
            type:'POST',
            url:'lib/sc-save.php',
            data:$data  
        }).done(function(){
            p++;
        });
    });
}

这些方面的东西都应该解决你的问题,并且是一个地狱更容易阅读/维护。

Something along these lines should both solve your problem, and be a hell of a lot easier to read / maintain.

编辑

有人指出, .each()函数已经为您处理了索引。

As someone pointed out, the index is already handled for you by the .each() function.

function SaveCombinations() {
    $('#sort li').each(function(i){
        var $id = this.id,
            $name = $(this).find('input').val(),
            $priority = i,
            $data = {id:$id,name:$name,priority:$priority};

        $.ajax({
            type:'POST',
            url:'lib/sc-save.php',
            data:$data                
        });
    });
}

这也适用于实施。

这篇关于带特殊字符的JQuery AJAX POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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