admin 管理员组文章数量: 1086019
I have a json object that I would like to post. I beleive that Im close but the data isnt getting sent correctly.
data is correctly formatted json string and it works correctly with ajax. However, the page needs to be redirected according to the REST api im requesting. Obviously using ajax, this wouldn't happen.
var data = JSON.stringify(myJsonObject);
$('<form enctype="application/json" action="/projects" method="POST">' +
'<input type="hidden" name="json" value="' + data + '">' +
'</form>').submit();
I have a json object that I would like to post. I beleive that Im close but the data isnt getting sent correctly.
data is correctly formatted json string and it works correctly with ajax. However, the page needs to be redirected according to the REST api im requesting. Obviously using ajax, this wouldn't happen.
var data = JSON.stringify(myJsonObject);
$('<form enctype="application/json" action="/projects" method="POST">' +
'<input type="hidden" name="json" value="' + data + '">' +
'</form>').submit();
Share
Improve this question
asked Oct 14, 2014 at 20:25
MichaelTaylor3DMichaelTaylor3D
1,6653 gold badges20 silver badges33 bronze badges
10
- 1 So you are using jQUery, but you can't use AJAX? Why this requirement? – Mike Brant Commented Oct 14, 2014 at 20:28
- You can redirect via the client – tymeJV Commented Oct 14, 2014 at 20:28
- @MikeBrant is right, use jQuery.Ajax – Mohamad Shiralizadeh Commented Oct 14, 2014 at 20:29
- I try this code, I don't know why it does not submit. maybe browser security stop this.. – Mohamad Shiralizadeh Commented Oct 14, 2014 at 20:33
- POST request is the same POST request no matter how you send it: AJAX, curl or form submit. – dfsq Commented Oct 14, 2014 at 20:33
4 Answers
Reset to default 4I think you need to escape your stringified JSON.
var data = $(JSON.stringify(myJsonObject);
function escapeHtml(text) {
var map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, function(m) { return map[m]; });
}
$('<form enctype="application/json" action="/projects" method="POST">' +
'<input type="hidden" name="json" value="' + escapeHtml(data) + '">' +
'</form>').submit();
Your JSON string contains quotes and it breaks the html.
Edit: You can also use escape(string) if you don't care if it is in readable format in the hidden input. Then you can use unescape(string) to get back your json string. That way you can use the same function to pass it over get requests too ;)
{name: "test"}
==> %7B%22name%22%3A%22test%22%7D
Example: https://stackoverflow./a/17696884/986160
What about Curl
you could post you data like so. You will need a ajax call the PHP function :
function sendData($json){
$url = 'wwww.exemple.';
//setting the curl parameters.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// Following line is pulsary to add as it is:
curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
This function send a json and get the return from API.
you can do this but id redirect the page. other solution is using $.Ajax or $.post
var form = $('<form enctype="application/json" action="/projects" method="POST">' +
'<input type="hidden" name="json" value="' + data + '">' +
'</form>');
form.submit();
本文标签: javascriptPost JSON data without ajax or formStack Overflow
版权声明:本文标题:javascript - Post JSON data without ajax or form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1743990198a2514581.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论