Angularjs - $http 发布数据中的等号 [英] Angularjs - equal sign in $http post data

查看:27
本文介绍了Angularjs - $http 发布数据中的等号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 angularjs 的 $http 服务将数据发布到我的 API.它工作得很好......直到我添加和等号到数据的内容(例如JSONRequest)

I'm using angularjs' $http service to post data to my API. It works great.. until I add and equals sign to the contents of data (JSONRequest in example)

var request = {
    'method': 'POST',
    'url': API_URL + apiActionName,
    'data': JSONRequest,
    'withCredentials': true,
};
$http(request).
success(function(data, status, headers, config) {
    // handle success
}).
error(function(data, status, headers, config) {
    // handle error
}

这适用于包含以下 JSONRequest 的数据

this works for data contain the following JSONRequest

{
    'text':'this is  some text'
}

但是当数据包含这个

{
    'text':'this is = some text'
}

请求被转义,服务器无法对 POST 做任何事情!!它似乎适用于所有其他字符.

the request is escaped and the server cannot do anything with the POST!! It seems to work with all other characters.

任何帮助将不胜感激!谢谢

Any help would be greatly appreciated! Thanks

推荐答案

它与 URI 编码有关.被发送到较低级别的 AJAX API 的字符串不是 uri 编码的.这意味着当它看到帖子中的 = 符号时,它正在解析它,假设为 uri 编码.

It had to do with URI encoding. The string that was getting getting sent the the lower level AJAX API was not uri encoded. This means that when it saw the = sign in the post it was parsing it assuming uri encoding.

可能没有提供所有信息来回答这个问题,因为 JSONRequest 最初是在 var request 上方的行中定义的.对于那个很抱歉.它的定义如下:

All of the information may not have been present to answer this question as JSONRequest was originally was defined on the line above var request. Sorry about that. It was defined as follows:

var JSONRequest = {
    'JSON': requestObject;
}

requestObject 在哪里

where requestObject was

{
    'text':'this is = some text'
}

我最终对 JSON 请求进行了 URI 编码,如下所示

I ended up URI encoding the JSON Request as follows

var JSONRequest = 'JSON=' + encodeURIComponent(JSON.stringify(requestObject));

这解决了问题并使我的 API 调用对 HTTP 协议更加健壮.

This fixed the problem and made my API calls more robust to the HTTP protocol.

感谢大家的帮助!!

这篇关于Angularjs - $http 发布数据中的等号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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