简体   繁体   English

如何更改URL参数格式

[英]How to Change URL parameter formats

when I pass parameters through url I want to change the format of them. 当我通过url传递参数时,我想更改它们的格式。

Is that possible? 那可能吗?

http://localhost:51406/home/Booking?Rooms=1&Destination={  "name": "Mumbai, India",  "code": "BOM"}&DepartureDate=12/18/2017&ReturnDate=12/19/2017&Adults1=1&Children1=0&C1Age1=0&C1Age2=0&C1Age3=0&C1Age4=0&Adults2=0&Children2=0&C2Age1=0&C2Age2=0&C2Age3=0&C2Age4=0&Adults3=0&Children3=0&C3Age1=0&C3Age2=0&C3Age3=0&C3Age4=0

I want to change this like this 我想这样改变

http://***/home/Booking?Rooms=1&Destination=%22Mumbai,%20India(BOM)%22&DepartureDate=12/18/2017&ReturnDate=12/21/2017&Adults1=1&Children1=0&C1Age1=0&C1Age2=0&C1Age3=0&C1Age4=0&Adults2=0&Children2=0&C2Age1=0&C2Age2=0&C2Age3=0&C2Age4=0&Adults3=0&Children3=0&C3Age1=0&C3Age2=0&C3Age3=0&C3Age4=0

the destination should change like second one 目的地应该像第二个一样改变

this is the searchcontroller.js 这是searchcontroller.js

app.factory("States", function () {
    var states = [ { 'name' : 'Ansan, South Korea', 'code': 'A1N' },
                   { 'name' : 'Asan, South Korea', 'code': 'A1S' },
                   { 'name' : 'Mumbai, India', 'code': 'BOM' },
                   { 'name' : 'Atascadero - CA, United States', 'code': 'AA1' },
                   { 'name' : 'London, United Kingdom', 'code': 'LON' },
                   { 'name' : 'Arlon, Belgium', 'code': 'AAO' } ] 
    return states; 
}); 

this is the view part code 这是视图的部分代码

   <input name="states" id="city"
          style="margin-left: 0px;margin-right: 109px; font-size: 14px" 
          type="text" placeholder="Any worldwide city or airport"
          ng-model="selectedNumberNonEditable" allow-custom="false"
          typeahead="state as state.name + ' (' + state.code + ')' for state in states | filter:$viewValue | limitTo:8"
          class="form-control" required>

ADDED from comment 已从评论中添加

<a id="sbutton" target="_blank" class="btn btn-primary btn-lg"
   style="background-color: #E28F13; font-size: 14px"
   ng-href="@Url.Action("Booking", "home")?Rooms={{rooms}}
            &Destination={{selectedNumberNonEdita‌​ble|json}}
            &Departure‌​Date={{departuredate‌​}}"
   onclick="validate(event);"> Search </a>

Your need to use encodeURI 您需要使用encodeURI

 var param = 'Destination={ "name": "Mumbai, India", "code": "BOM"}'; var encodedParam = encodeURI(param ); console.log(encodedParam ); 

Or 要么

Are you looking something like this 你看起来像这样吗

 function getParameterByName(name, url) { if (!url) url = window.location.href; name = name.replace(/[\\[\\]]/g, "\\\\$&"); var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"), results = regex.exec(url); if (!results) return null; if (!results[2]) return ''; return decodeURIComponent(results[2].replace(/\\+/g, " ")); } var url = 'http://localhost:51406/home/Booking?Rooms=1&Destination={ "name": "Mumbai, India", "code": "BOM"}&DepartureDate=12/18/2017&ReturnDate=12/19/2017&Adults1=1&Children1=0&C1Age1=0&C1Age2=0&C1Age3=0&C1Age4=0&Adults2=0&Children2=0&C2Age1=0&C2Age2=0&C2Age3=0&C2Age4=0&Adults3=0&Children3=0&C3Age1=0&C3Age2=0&C3Age3=0&C3Age4=0'; var destination = JSON.parse(getParameterByName("Destination", url)); var newDestination = destination.name + "(" + destination.code + ")"; console.log(newDestination); 

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

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