Transfer two values in Ajax to server



  • There's a Ajax that has to transmit two values. I only did one thing.

    $.ajax({
        type: 'POST',
        url: "/ajax/search_bid_country",
        data: "city_from="+city_from,
        success: function(answer) {
            $(".result_article").empty();
            $(".result_article").append(answer);
            $(".result_article").fadeIn();
            $(".load").css({display:"none"});
        }
    });
    

    But the other one can't make a mistake,

    data: "city_from="+city_from, "city_whre="+city_where,
    

    How to be



  • Of course there will be a mistake, comma in your example means moving to another object. You need to behave. data Specified line or object. It's clearly spelled out. http://api.jquery.com/jQuery.ajax/ ♪

    In your case, it's just, instead of a comma, to use the content:

    data: "city_from=" + city_from + "&city_where=" + city_where,
    

    Or you'd better hand over the object:

    data: {city_from: city_from, city_where: city_where},
    

    But what happens when the parameters get bigger? There'll be a lot of unnecessary writing. Use better. http://api.jquery.com/serialize/ ♪

    data: $("form").serialize(),
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2