博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
$.post html5,ajax - HTML5 Post Request Body - Stack Overflow
阅读量:5171 次
发布时间:2019-06-13

本文共 2076 字,大约阅读时间需要 6 分钟。

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!

转载地址:http://ahhiv.baihongyu.com/

你可能感兴趣的文章
给一次重新选择的机会_您还会选择程序员吗?
查看>>
Mysql MHA高可用集群架构
查看>>
心急的C小加
查看>>
编译原理 First,Follow,select集求法
查看>>
iOS开发 runtime实现原理以及实际开发中的应用
查看>>
android 学习资源网址
查看>>
qt安装遇到的错误
查看>>
java:Apache Shiro 权限管理
查看>>
objective c的注释规范
查看>>
FreeNas安装配置使用
查看>>
Django(一)框架简介
查看>>
Python操作SQLite数据库的方法详解
查看>>
菜单和工具条(二)
查看>>
hadoop17---RPC和Socket的区别
查看>>
使用JMeter代理录制app测试脚本
查看>>
Linq to Object实现分页获取数据
查看>>
mac常用系统命令
查看>>
android上传文件到服务器
查看>>
我回答了90%的面试题,为什么还被拒?
查看>>
Html - Table 表头固定和 tbody 设置 height 在IE不起作用的解决
查看>>