$.post html5,ajax - HTML5 Post Request Body - Stack Overflow

Firstly, I suggest rewriting your code with jQuery's help. This would compact your code, make it cross-platform, and easier to read and maintain:

function sendPost(){

$.ajax({

url: "some url",

type: "POST",

contentType: "text/xml",

data:

"005693" +

"0.00.0" +

"SROMNMPRELOGIN" +

"PRELOGIN/IDPRELOGIN" +

"005693" +

"" + $("#username").val() + "" +

"" + $("#password").val() + "" +

"",

dataType: 'xml',

success: function(data) {

var firstname = $(data).find("FIRSTNAME").text();

var lastname = $(data).find("LASTNAME").text();

alert('Hello ' + firstname + ' ' + lastname);

},

error: function(jqXHR, textStatus, errorThrown) {

alert('Error');

}

});

}

Secondly, a javascript that origins from your server (e.g. www.myserver.com) can't communicate with other servers (i.e. you can't request data from www.anotherserver.com). Well you CAN, but if so you'd need to ensure the answer sent from www.anotherserver.com would be in JSONP format - and then you would just change "dataType" in the example above to "jsonp" to be able to access the result like "data.firstname" and "data.lastname".

Anyway, in your case I would create a local proxy on my own webserver (in the same folder where you have the above .HTML-file) that would forward the request to the other server and return the result. Thus:

$.ajax({

url: "myproxy.php",

type: "POST", ...

And then in myprox.php, something like this (I'm just assuming PHP here, but this could be easily ported to ASP.NET or ASP Classic):

// myproxy.php forwards the posted data to some other url, and returns the result

$clientContext = stream_context_create(array(

'http' => array(

'method' => 'POST',

'header' => 'Content-Type: text/xml; charset=utf-8',

'content' => http_get_request_body()

)

));

print file_get_contents("some url", false, $clientContext);

?>

To clarify: This would make your HTML-page talk to myproxy.php (which lives on the same server [even in the same directory]), then myproxy.php talks to the server at "some url" which returns the data to myproxy.php, which in it's turn returns the data to your script.

Best of luck!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值