Angularjs - 在$ HTTP POST数据等号等号、数据、Angularjs、HTTP

2023-09-13 03:41:40 作者:吻尔之眸

我使用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'
}

然而,当数据包含此

however when the data contains this

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

请求逃脱,服务器不能做的张贴任何!这似乎与所有其他角色的工作。

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

任何帮助将大大AP preciated!谢谢

Any help would be greatly appreciated! Thanks

推荐答案

据曾与URI编码做。这是越来越得到发送的下级AJAX API不开放的EN codeD的字符串。这意味着,当它看到=在它被解析它假设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.

所有的信息可能不是present来回答这个问题,因为JSONRequest最初于上述VAR要求该行定义。对于那个很抱歉。它被定义如下:

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'
}

我结束了URI编码JSON请求如下:

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.

感谢所有帮助!