HTML page
// in script tag
$(document).ready(function () {
var url = "list.ashx";
$.get(url + "?get", function (r1) { alert("get: " + r1); });
$.post(url + "?post", function (r2) { alert("post: " + r2); });
$.ajax(url + "?ajax", function (r3) { alert("ajax: " + r3); });
$("div:last").load(url + "?load", function (r4) { alert("load: " + r4); });
});
// in body tag
<div></div>
in 'list.ashx'
public void ProcessRequest (HttpContext context) { context.Response.Write("ok"); }
the result
- $.get and $.post reach list.ashx but no return
- $.ajax not reach list.ashx
- $.load fully success
The problems are
- why only '$.load' work?
- how to make $.get or $.post work?
update
$("input").click(function () {
$.ajax({ url: url
, context: this
, data: "ajax=test"
, cache: false
, async: false
, global: false
, type:"POST"
, processData: false
, dataType: "html"
, success: function (data) { alert(data); }
, error: function (data) { alert(data.responseText); }
});
});
it's always hit error:function(){} but the 'data.responseText' is the correct result!!
Well, the reason your $.ajax()
doesn't work is because it's syntactically invalid. It should look more like this:
$.ajax({
type: "POST", // or "GET"
url: "list.ashx",
data: "postvar=whatever",
success: function(r3){
alert("ajax: " + r3);
}
});
Also, when using $.get
and $.post
, you should put the data in the second parameter:
$.get(url, 'getvar=whatever', function (r1) { alert("get: " + r1); });
$.post(url, 'postvar=whatever', function (r2) { alert("post: " + r2); });
// or use a map
$.get(url, { getvar : 'whatever' }, function (r1) { alert("get: " + r1); });
$.post(url, { postvar : 'whatever' }, function (r2) { alert("post: " + r2); });
karim79
Since you are firing off four asynchronous requests in one go to the same page, this might be relevant:
来源:https://stackoverflow.com/questions/4450139/get-post-ajax-elm-load-to-ashx-page-problem