开发者

jquery ajax response removes the + character

开发者 https://www.devze.com 2022-12-11 03:59 出处:网络
Update: It looks like the problem is when I\'m reading the value from the html to begin with. This is the markup:

Update:

It looks like the problem is when I'm reading the value from the html to begin with.

This is the markup:

<input name="someval" type="text" value="Receive (+ open)" />

I'm reading the value of someval like this and passing it to the ajax call:

    $.ajax({

        data: 'someval=' + $("input[name=someval]").val(),

        success: function(resp) {
           $("#targetd").html(resp);
           alert(resp); 
        }
});

Looks like the problem happens when it concatenates $("input[name=someval]").val() to data: 'someval=' + since $("input[name=someval]").val() has a + in it.

Old

I'm making an ajax call to a php script and the response returned contains html markup. I take that response and assign it to a div after that.

success: function(resp) {
   $("#targetd").html(resp);
   alert(resp); 
}

Part of the r开发者_运维技巧esponse being returned is supposed to look like this. (notice the + sign)

<input name="someval" type="text" value="Receive (+ open)" />

It's working fine and I can assign it to the div, except that for some reason the markup returned is missing the + sign

<input name="someval" type="text" value="Receive (  open)" />

When the php script itself outputs the markup, it works fine, but when the markup is passed to the page through an ajax response the + disappears. I'm not doing anything special to the markup, as the code above shows, I'm just taking it as is as soon as it's returned and assigning it to the div. When I alert the response directly, the + is also missing so not just when it gets assigned to the div. Not sure what could be going on here.


Did you try HTML encoding the response? I use these functions in my javascript to encode/decode.

function HtmlEncode(value)
{
    return $('<div/>').text(value).html();
}

function HtmlDecode(value)
{
    return $('<div/>').html(value).text();
}


I recommend trying to escape your plus + as &#43; and see if that solves your problem. If it does, its a entity escape problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消