开发者

Ajax call to pass some parameters

I got the below function trigger by a button on my webpage.

I need to pass a value from a text field to the ajax call. I have the below script but nothing is occuring.

function sentDateToS开发者_StackOverflow社区erver(){
        var runDate = $('#rundate').html();
        $.ajax({
            type: 'GET',
            url: '/clientsurvey/planner.html?runDate=' + runDate,
            success:  function(response) {

            }
            error:  function(xhr, asdf,) {

            }
        });


Assuming that you actually mean "input element" when you say "text field", then you should use the val() function to get its value, not the html() which would return the nested HTML content (there is indeed usually none in case of input elements).

var runDate = $('#rundate').val();

Don't forget to encodeURIComponent() it or, better, use the data option in $.ajax() settings.


First, It could be that you need a comma after the success callback close bracket. Perhaps because this comma isn't there the ajax function doesn't know how to close itself therefore jquery ignores the function entirely.

Secondly, I agree with @Trevor's confusion about using /clientsurvey/planner.html? as your url for this request, this should be php or foxpro or something. If it is one of these languages then you don't even need the extension, you could just do '/clientsurvey/planner?runDate='+runDate and that should work.

Third, it is syntactically smarter to place the error callback before the success because if the ajax function throws an error then there is no sense in jQuery wasting memory and processing capability on reading in functions that will not executed.

so, your statement changes to the following:

function sentDateToServer(){
    var runDate = $('#rundate').html();
    $.ajax({url: '/clientsurvey/planner?runDate=' + runDate,
        type: 'GET',
        error: function(xhr, asdf,){
            alert(xhr + "\n\n" + asdf);
        },
        success:  function(response){
            //do something
        }
    });
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜