[英]How do I get the specific part of a URL using jQuery and JavaScript?
I am checking out an order in WordPress via jQuery AJAX. 我正在通过jQuery AJAX在WordPress中签出订单。 Upon successfully posting, WordPress returns a response to me with a result
success
value and url
value. 成功发布后,WordPress将结果
success
值和url
值返回给我。
I want to get the particular part of this url
so I can use the id
as an object for my purpose. 我想获取此
url
的特定部分,以便可以将id
用作对象。
This is the structure of the url: 这是网址的结构:
http://localhost/mywebsite/checkout/order-received/28564?key=wc_order_5b4dbc6b2f459 http:// localhost / mywebsite / checkout / order-received / 28564?key = wc_order_5b4dbc6b2f459
This is my current code: 这是我当前的代码:
j('.my-checkout').on('submit', function(evt) {
evt.preventDefault();
var billing_customer_type = j("#billing_customer_type").val();
// and so on...
j.ajax({
type: 'POST',
url: 'http://localhost/mywebsite/ajax=checkout',
cache: false,
data: {
'billing_customer_type': billing_customer_type,
// and so on..
},
success: function(result) {
var orderResponseUrl = result.redirect;
j('.order-response-url').html(orderResponseUrl);
// http://localhost/mywebsite/checkout/order-received/28564?key=wc_order_5b4dbc6b2f459
orderResponseUrl.split("/");
console.log(orderResponseUrl[3]);
},
error: function(xhr, status, error) {
console.log(error);
},
complete: function() {}
});
});
The result of my code above is just the the letter "p". 我上面的代码的结果就是字母“ p”。 I think because it started of the first letter of
http
and I used the index [3]
. 我认为是因为它以
http
的首字母开头,所以我使用了索引[3]
。
Do you know how can I get the specific part of the url
that is 28564
? 您是否知道如何获取
url
的特定部分28564
?
Because when you do orderResponseUrl.split("/");
因为当您执行
orderResponseUrl.split("/");
it does NOT change orderResponseUrl
, it creates a new array. 它不会更改
orderResponseUrl
,而是创建一个新数组。
var parts = orderResponseUrl.split("/");
console.log(parts);
if the length is always same then you can use substring function. 如果长度始终相同,则可以使用子字符串功能。
var str = "Hello world!";
var res = str.substring(1, 4);
now res contain 现在资源包含
console.log(res); // ell
if you do not know the index, you can find like this. 如果您不知道索引,则可以找到这样的索引。
var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
now n look like 现在看起来
console.log(n); // 13
If the URL that you need to interact with is always the same, you can split the returned url at the 'order-received/' portion (which gives an array of everything before that, and everything after it). 如果您需要与之交互的URL始终相同,则可以在“ order-received /”部分拆分返回的url(这提供了之前和之后的所有内容的数组)。
Then rather than splitting again on the '?" which is another way of doing it - you can use parseFloat()
to get the order number. This works since parseFloat returns all numerical values up to but not including the first non-numerical character ("?"). 然后,您可以使用
parseFloat()
来获取订单号,而不是再次在'?'上拆分,因为parseFloat返回所有数值,但不包括第一个非数字字符( “?”)。
var urlStr = 'http://localhost/mywebsite/checkout/order-received/28564?key=wc_order_5b4dbc6b2f459'; var orderNumber = parseFloat(urlStr.split('order-received/')[1]); console.log(orderNumber); //gives 28564
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.