简体   繁体   中英

How to send data in HTTP POST request while calling a url?

I wrote a simple jsp page where I am loading an external website inside one of my division.

Below is the code :

<html>
<head>
<script src="../common/jquery-1.6.2.min.js"></script>
<script>
  $(document).ready(function(){
       $("#menu").html('<object data="www.someurl.com">').appendTo('body');;
   });

</script>
</head>

<body>
<div id="menu" style="position:relative; bottom: 0; overflow:hidden;">
</div>
</body>

</html>

Basically what I am doing is that , I am setting an HTML content having a src URL inside the object tag. This is successful and I can load web URL inside my division.

Question comes when I want to send some data(may be a single variable) using HTTP POST method.

Is there any way to call the same URL using HTTP POST ?

Note : Here I cannot use Jquery POST because of same origin policy.

Take a look at the Jquery post function: http://api.jquery.com/jQuery.post/

Also, if you want to send some data, along with the url, you can do it with your get as well:

www.someurl.com?dataAttr=someValue&dataOtherAttr=someOtherDataValue

This would be the GET equivalent of a post with the following data:

{ 
  "dataAttr": "someValue",
  "dataOtherAttr": "someOtherDataValue"
}

Here is a pure js that will make a post to a server. The script makes an invisible iframe and makes a call to a specified server with the specified parameters. The server has to support the Cross Domain Policy. If you can't make the server support CORS, you are out of luck:

    /**
     * 
     * Makes a post to the specified url with the specified param - keyval
     */
    makePost  = function makePost(url, params){
      var iframeId = 'iframeid';
        var addParamsToForm = function (form, params){
            var addParamToForm = function(form, paramName, paramValue){
                var input = document.createElement('input');
                input.hidden = 'hidden';
                input.name = paramName;
                input.value = paramValue;
                form.appendChild(input);
            }
            for ( var prop in params ){
                if ( params.hasOwnProperty(prop) ){
                    if ( params[prop] instanceof Array ){
                        for ( var i = 0; i < params[prop].length; i ++ ){
                            addParamToForm(form, prop, params[prop][i]);
                        }
                    } else {
                        addParamToForm(form, prop, params[prop]);
                    }
                }
            }
        };
        var iframe = document.getElementById(iframeId);
        if ( iframe === null ){
            iframe = document.createElement('iframe');
            iframe.name = 'iframeName';
            iframe.id = iframeId;
            iframe.setAttribute("style", "width: 0; height: 0; border: none; display: none;");
        }
        var form = document.createElement('form');
        form.action = url;
        form.method = 'POST';
        form.target = iframe.name;

        addParamsToForm(form, params);

        iframe.appendChild(form);
        document.getElementsByTagName('body')[0].appendChild(iframe);
        form.submit();
    }

example usage:

makePost('yourserver', {'someAttr':'someAttrValue', 'someOtherAttr': 'someOtherAttrValue'});

Or a jquery variant:

$.ajax({
    type: 'POST',
    url: 'yourserver',
    crossDomain: true,
    data: {'someAttr':'someAttrValue', 'someOtherAttr': 'someOtherAttrValue'},
    dataType: 'json',
    success: function(responseData, textStatus, jqXHR) {
        var value = responseData.someKey;
    },
    error: function (responseData, textStatus, errorThrown) {
        alert('POST failed.');
    }
});

On tips how to configure your server to support CORS:

http://enable-cors.org/server.html

Take a look at this one:

How do I send a cross-domain POST request via JavaScript?

Try to load the external link or website in an Iframe and try.

 <div>
   <iframe id="" name="" src="your external link" ></iframe>
 </div>

I dont know if you an use php,becouse of the no php tag,but you can post data using HttpRequest class in php,and it is secured ! here is a link : http://php.net/manual/en/class.httprequest.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM