【问题标题】:Correct URL in ajax call JavaScriptajax 调用 JavaScript 中的正确 URL
【发布时间】:2017-12-01 15:21:04
【问题描述】:

我有一个 GET ajax 调用如下:

var changeUrl = "changePriority?newValue=" + targetValue + "&justification=" + justification
if (dataInfo == "row") {
    changeUrl += "&id=" + id
}
changeUrl += "&executedConfigId=" + executedConfigId + "&currUser=" + currentUser + "&productName=" + productName + "&eventName=" + eventName + "&alertDetails=" + JSON.stringify(alertArray);
//if the selected signal is not null then we show the product names

$.ajax({
    url: changeUrl,
    type: "GET",
    success: function (data) {
        for (var index = 0; index < checkedRowList.length; index++) {
            var row = checkedRowList[index]
            signal.list_utils.change_priority(row, targetValue);
        }
        $('#change-priority-modal').modal('hide');
        if (applicationName == "Signal Management") {
            signal.list_utils.set_value(parent_row, 'dueIn', id, signal.list_utils.get_due_in, applicationName);
            $(parentField).html(targetValue);
        }
        location.reload();
    },
    error: function (exception) {
        console.log(exception);
    }
});

我在浏览器的开发者控制台中得到的changeUrl 的值是:

http://localhost:8080/signal/singleCaseAlert/changePriority?newValue=Medium&amp;justification=test%20justification%20first.&amp;id=6816&amp;executedConfigId=6704&amp;currUser=15&amp;productName=Wonder%20Product&amp;eventName=1.Pyrexia&amp;alertDetails=[{%22alertId%22:%226816%22,%22event%22:%221.Pyrexia%22,%22currentUser%22:%2215%22}]

但我在后端收到 400 错误请求状态和 http 标头解析错误。有人可以帮我解决这个问题吗?

【问题讨论】:

  • ; 放在第一行的末尾以及第三行
  • 首先检查错误日志以找出为什么您得到 400。
  • @YashParekh 输入分号没有帮助。
  • @CBroe 我已经尝试解析 url 它正确地提供了所有查询字符串参数。

标签: javascript ajax http url


【解决方案1】:

在您的 JSON.stringify(alertArray) 上,您还需要 encodeURI();

encodeURI(JSON.stringify(alertArray));

如果在您的设计中可行,更好的解决方案是将您的 JSON 发送到 POST 请求的正文中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-08
    • 2016-10-10
    • 1970-01-01
    • 2013-12-31
    • 2020-03-08
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    相关资源
    最近更新 更多