jQuery的Ajax响应删除字符+字符、jQuery、Ajax

2023-09-10 21:46:16 作者:不雨花犹落

更新:

它看起来像问题是,当我在读从HTML价值的开始。

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

这是标记:

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

我读 someval 这样的价值,将其传递给Ajax调用:

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); 
        }
});

看起来像时,它并置 $(输入[名称= someval])VAL()数据发生问题。someval ='+ ,因为 $(输入[名称= someval])。VAL()有一个 + 在里面。

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

我在做一个Ajax调用PHP脚本和响应返回包含HTML标记。我采取的响应,并将其分配给后一个div。

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 response being returned is supposed to look like this. (notice the + sign)

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

它的正常工作,我可以把它分配到div,但由于某种原因,返回的标记缺少 + 标志

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

在PHP脚本本身输出的标记,它工作正常,但是当标记是通过Ajax响应传递到页面的 + 消失。我没有做什么特别的标记,为code以上节目,我只是把它作为只要它是返回并将其分配到div。当我直接提醒响应, + 也缺少这样不只是当它被分配到股利。不知道什么可以怎么回事。

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.

推荐答案

您是否尝试过HTML编码的回应?我用我的JavaScript这些功能来连接code /德code。

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();
}