简体   繁体   English

如何在特定模式下使用javascript附加网址

[英]How to append url using javascript in particular pattern

I want to append variable data in js in below code in url 我想在下面的URL代码中添加js中的变量数据

$(document).on('change','.sort_rang',function(){
   var url = "ajax_search.php";
   //console.log($("#search_form").serialize());
   var data = $("#search_form").serialize();
   //data += "&pn="+ <?php echo $_GET['pn']; ?>;
   //console.log(data);
   $.ajax({ 
     type: "POST",
     url: url,
     data: data,
     success: function(response)
     {                  
        $('.ajax_result').html(response);
     }               
   });

  return false;
});

How to append the url in below format, 如何以以下格式附加网址,

?pg=2&company=motorola,lenovo&pricerange=2 I want to append url in ajax_search.php ?pg=2&company=motorola,lenovo&pricerange=2我想在ajax_search.php中附加网址

after var_dump($_REQUEST) . var_dump($_REQUEST) I'm getting this 我得到这个

array(4) { ["company"]=> array(1) { [0]=> string(6) "Lenovo" } 
["category"]=> array(1) { [0]=> string(6) "mobile" } ["pricerange"]=> 
string(1) "1" ["pricesort"]=> string(1) "1" }

From this i want to append on above format 从这个我想附加上面的格式

If your service ( ajax_search.php ) requires the GET method then you can simply change the type parameter of your $.ajax request from type: "POST" to type: "GET" then jQuery does the job, you don't need to append the string to the URL by hand. 如果您的服务( ajax_search.php )需要GET方法,则只需将$.ajax请求的type参数从type: "POST"更改为type: "GET"然后jQuery即可完成工作,您无需手动将字符串附加到URL。

$(document).on('change','.sort_rang',function(){
    var url = "ajax_search.php";
    //console.log($("#search_form").serialize());
    var data = $("#search_form").serialize();
    //data += "&pn="+ <?php echo $_GET['pn']; ?>;
    //console.log(data);
    $.ajax({ 
        type: "GET", // <-- Note the change here from POST to GET
        url: url,
        data: data,
        success: function(response) {                  
            $('.ajax_result').html(response);
        }               
    });
    return false;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM