开发者

Need jQuery.load() to return HTML but server-side handler method only accepts POST method?

What do you do if you call the load method like so:

$('#result').load('/return_r开发者_如何学Goesult');

and you get a server error that says

Request method 'GET' not supported

load() seems perfect for what I want to do - return HTML from the server and put it into the #result element.


use post instead :)

$.post('/return_result', function(data){
    $('#result').empty().html(data);
});


load by defaults use the HTTP method as GET. Looks like the url /return_result doesnot accept GET. If it is your service make sure it supports GET. or use .ajax() method of jQuery to set the type to what ever method it supports.


You could use the $.ajax() method instead, as you can control what HTTP verb it will use.

$.ajax({
   type: "POST",
   url: "/return_result",
   success: function(msg){
      alert( "response: " + msg );
   }
});


If your server only supports POST, then Just add a dummy data string to be posted like this,

$('#result').load('/return_result', {test: 10});

Then the method will switch to using POST method. But this may not be the most elegant solution.


The parameter of the .load() method must be some representation of HTML code, whether it be a div, or an entire page. It seems as if '/return_result' doesn't meet those requirements. If I am wrong, and the element does contain HTML, try using the jQuery $('#div').empty() and $('#div').appendTo('destination') methods.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜