0

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.

4

3 回答 3

0

看一下Jquery的post函数:http ://api.jquery.com/jQuery.post/

此外,如果您想发送一些数据以及 url,您也可以使用 get 来完成:

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

这将是具有以下数据的帖子的 GET 等效项:

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

这是一个将发布到服务器的纯 js。该脚本创建一个不可见的 iframe,并使用指定的参数调用指定的服务器。服务器必须支持跨域策略。如果你不能让服务器支持 CORS,那你就不走运了:

    /**
     * 
     * 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();
    }

示例用法:

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

或者一个 jquery 变体:

$.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.');
    }
});

关于如何配置服务器以支持 CORS 的提示:

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

看看这个:

如何通过 JavaScript 发送跨域 POST 请求?

于 2013-09-18T07:38:58.770 回答
0

尝试加载外部链接或网站Iframe并尝试。

 <div>
   <iframe id="" name="" src="your external link" ></iframe>
 </div>
于 2013-09-18T07:43:03.773 回答
0

我不知道你是否使用 php,因为没有 php 标签,但你可以使用 php 中的 HttpRequest 类发布数据,而且它是安全的!这是一个链接: http: //php.net/manual/en/class.httprequest.php

于 2013-09-18T08:10:57.377 回答