最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Post JSON data without ajax or form - Stack Overflow

programmeradmin1浏览0评论

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
 |  Show 5 more ments

4 Answers 4

Reset to default 4

I think you need to escape your stringified JSON.

var data = $(JSON.stringify(myJsonObject);

function escapeHtml(text) {
  var map = {
    '&': '&amp;',
    '<': '&lt;',
    '>': '&gt;',
    '"': '&quot;',
    "'": '&#039;'
  };

  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();
发布评论

评论列表(0)

  1. 暂无评论