URL在ajax调用中编码

时间:2017-01-31 09:18:33

标签: javascript php jquery ajax

var pid = $('#pid').val();
        var pname = $('#pname').val() + " " + $('#varient_title').val() + " " + $('#varient_value').val();
        var price = $('#price').val();
        var shipping = $('#shipping').val(); 
  $.ajax({
        url: "ajax/cart.php?pid=" + pid + "&pname=" + pname + "&price=" + price + '&shipping=' + shipping,
        type: "GET",

在ajax调用中我想编码pname ...

2 个答案:

答案 0 :(得分:1)

  

试试这个

var pid = $('#pid').val();
    var pname = $('#pname').val() + " " + $('#varient_title').val() + " " + $('#varient_value').val();
    var price = $('#price').val();
    var shipping = $('#shipping').val(); 
$.ajax({
    url: "ajax/cart.php?pid=" + pid + "&pname=" + encodeURI(pname) + "&price=" + price + '&shipping=' + shipping,
    type: "GET",

答案 1 :(得分:0)

您可以使用POST发送数据。您也可以将类型更改为GET。你也是json格式的编码和解码pname参数:

    var pid = $('#pid').val();
    var price = $('#price').val();
    var shipping = $('#shipping').val(); 

    var pname = {
         pname: $('#pname').val(),
         varient_title: $('#varient_title').val(),
         varient_value: $('#varient_value').val()
    };

    $.ajax({
        url: "ajax/cart.php",
        type: 'POST',
        datatype: 'html',
        data: {
            pid: pid ,
            pname : JSON.stringify(pname),
            price : price,
            shipping: shipping,
        },
        success: function(data) {
            console.log("success")
        },
        error: function(jqXHR, textStatus, errorThrown) {
            console.log('Ajax', 'Error', textStatus);
        },
    });