-6

我找不到任何明确的例子来说明我想要什么。我想访问sample.aspx并希望通过POST(不是查询字符串)向它发送一些纯文本参数。如果成功,我想查看 JSON 格式的响应。如果它失败了,我想做一些事情来处理它。所以我需要成功和失败的功能。

我该怎么做呢?

4

2 回答 2

0

通过 POST 的纯文本参数(不是查询字符串).. 请详细说明

让我为你分解一下……

现在ajax是如何工作的

1-您发送请求(GET / POST)。

注意:访问网页是一个获取请求

2-页面输出响应..

3- jquery 读取页面 .. 它读取页面的 html .. 所以如果我用 $.ajax 查看堆栈溢出,我将获得完整的首页 html ..

这是一个例子

$.ajax({
  url: "http://stackoverflow.com",
  type: "GET",
  data: {id : 'myid'}, // the url will become http://stackoverflow.com?id=myid
  dataType: "html", // what type of response your expecting 
  success : function(e){ // e is the response 
              console.log(e); // the will log the html of stackoverflow
             }
});

但是如果您希望您将获得的数据是 json,那么您在服务器端所要做的就是使页面输出(显示)您想要的 json 作为字符串,jquery 将读取它,您可以将其解析为 json

这是一个小的 php 示例

<?php
echo 'hi ' . $_GET['id'] ;
?>

使用上面的 jquery 代码和这个页面我会得到响应

'hi myid'
于 2013-03-01T20:02:20.473 回答
-1
var data = ; //POST PARAMS for send 

$.ajax({
    url: '/sample.aspx',
    type: 'POST',
    contentType: "application/json",
    timeout: 10000,
    dataType: "html",
    data: data,
    success: function (response) {

    },
    error: function (error) {

    }
});
于 2013-03-01T19:56:25.323 回答