Javascript 如何获取POST方法“$.POST()”的结果?

Javascript 如何获取POST方法“$.POST()”的结果?,javascript,jquery,Javascript,Jquery,我使用以下代码发布数据: $.post("http://domain/page.aspx", postdata); 更新: 以下代码不起作用: $.post("http://domain/page.aspx", postdata, function(data){alert(data);}); 如何以字符串形式获取服务器响应?使用回调函数 $.post("http://domain/page.aspx", postdata, function(result) { alert(resul

我使用以下代码发布数据:

$.post("http://domain/page.aspx", postdata);
更新:

以下代码不起作用:

$.post("http://domain/page.aspx", postdata, function(data){alert(data);});

如何以字符串形式获取服务器响应?

使用回调函数

$.post("http://domain/page.aspx", postdata, function(result) {
    alert(result);
    $('divID').html(result);
});
$.post()
在文档中有以下描述:

说明:使用HTTP POST请求从服务器加载数据

 jQuery.post( url [, data ] [, success(data, textStatus, jqXHR) ] [, dataType ] )
在哪里,

url

    Type: String
    A string containing the URL to which the request is sent.
数据

    Type: PlainObject or String
    A plain object or string that is sent to the server with the request.
成功(数据、文本状态、jqXHR)

所以


返回JSON数据。因此,在那里使用您的数据类型,并相应地使用该函数。

我希望您遇到了,这会阻止ajax帖子跨源发布,除非服务器上支持并配置了ajax,以允许来自页面源的请求(并且使用的浏览器支持它)。

来自您的评论

好的,我明白了。当我在帖子后放置警告时,帖子处于“扩展”状态如果我删除警报页面正在更改(很难停止)。重定向前帖子的状态为“已取消”。

我知道您在点击某个链接后拨打了
.post
电话。您需要取消单击事件,以便不跟随链接

所以如果你有一些代码,比如

$('a').click(function(){
  $.post("http://domain/page.aspx", postdata, function(data){alert(data);});
});
换成

$('a').click(function(e){ // added e as parameter which get the event
  e.preventDefault(); // added this line which cancels the default action of the click
  $.post("http://domain/page.aspx", postdata, function(data){alert(data);});
});

它会抛出什么错误?没有任何错误。什么都没有。打开IE开发工具条,检查
网络
选项卡并尝试捕获您获得的输出。我只使用FTP测试这个简单脚本(没有其他工具)。@Altaveron:“我只使用FTP测试这个简单脚本(没有其他工具)。”这是荒谬的。例如,您有一个web浏览器(您正在使用它发布堆栈溢出)。FTP与HTML页面中执行的JavaScript代码无关。OP说的不正是这样吗?所有这些都不能回答OP的问题。@t.J.Crowder问题是获取字符串数据作为响应。不是吗?因此,我试图让用户熟悉$.post()参数,这些参数也可以接受MIME类型。@Bhushan:如果你看一下OP的代码,这正是你要演示的。所以这没有用,它没有回答问题。@t.J.Crowder在$.post()函数中包含数据类型不会有什么不同吗?@Bhushan:我们怎么知道?我们不知道OP想要完成什么。但是,如果响应没有中断,就不会。在
ajax
调用中使用
dataType
的唯一原因是您的服务器代码被破坏,并且没有正确返回
内容类型
标题。谢谢。结果:
此请求没有可用的响应数据
。但其他开发人员可能会看到它-服务器工作得很好。我不明白答案,但它似乎比其他人更正确。
$('a').click(function(){
  $.post("http://domain/page.aspx", postdata, function(data){alert(data);});
});
$('a').click(function(e){ // added e as parameter which get the event
  e.preventDefault(); // added this line which cancels the default action of the click
  $.post("http://domain/page.aspx", postdata, function(data){alert(data);});
});