简体   繁体   English

在AJAX调用中编辑URL

[英]Editing URL in AJAX call

I am trying to edit a URL , that i sent in AJAX call. 我正在尝试编辑AJAX呼叫中发送的URL。

function saveEdit() {
  // logic for updating an existing item
                $.ajax({
                url: "https://localhost/api/1/databases/geolocation/collections/boom/"+globalData._id.$oid+"?apiKey=veTqID_gkb74tG-yL4MGcS1p2RRBP1Pf",
                type: "PUT",
                data: JSON.stringify({  Item:Item.value , Location: Location.value , Category: Category.value , Description : Description.value , Quantity: Quantity.value}),
                contentType: "application/json"
            });

In the above code i want to replace "localhost" from an IP , when the website is UP. 在上面的代码中,当网站启动时,我想从IP替换“ localhost”。 I will read the IP from a config file. 我将从配置文件中读取IP。

Tried concatenating strings , did not work. 尝试连接字符串,无效。 Any suggestions? 有什么建议么?

Expected is : 预期是:

https://x.x.x.x/api/1/databases/geolocation/collections/boom/

Try this: 尝试这个:

function saveEdit() {
    var useLocalHost = false; // flag to taest if you want to do the replace or use the localhost parameter
    var localhostUrl = "https://localhost/api/1/databases/geolocation/collections/boom/"+globalData._id.$oid+"?apiKey=veTqID_gkb74tG-yL4MGcS1p2RRBP1Pf";
    var url = useLocalHost ? localhostUrl : localhostUrl.replace("localhost","x.x.x.x");

    // logic for updating an existing item
    $.ajax({
        url: url,
        type: "PUT",
        data: JSON.stringify({  Item:Item.value , Location: Location.value , Category: Category.value , Description : Description.value , Quantity: Quantity.value}),
        contentType: "application/json"
    });
}

Try String.prototype.replace() 试试String.prototype.replace()

var url = "https://localhost/api/1/databases/geolocation/collections/boom/"+globalData._id.$oid+"?apiKey=veTqID_gkb74tG-yL4MGcS1p2RRBP1Pf"
if(url.indexOf(UP) !== -1){
    var IP = GetIPfromConfig();
    url = url.replace(/localhost/, IP);
}

First off, how about using parameters: 首先,如何使用参数:

function saveEdit(host, key) {
  // logic for updating an existing item
                $.ajax({
                url: "https://" + host + "/api/1/databases/geolocation/collections/boom/"
                     + globalData._id.$oid + "?apiKey=" + key,
                type: "PUT",
                data: JSON.stringify({  Item:Item.value ,
                                        Location: Location.value ,
                                        Category: Category.value ,
                                        Description : Description.value ,
                                        Quantity: Quantity.value}),
                contentType: "application/json"
            });

However, now you've pushed off where to get host out of the saveEdit function into the caller. 但是,现在您已经完成了将host从saveEdit函数中移到调用程序中的位置。

If host is the same host that is serving the page that's easy, it's just 如果主机与提供该页面的主机相同,那很简单
saveEdit( window.location.hostname, myApiKey )

If it's some other host, from a config file as you say (where is the config file? on your server?), you have to somehow get that to the javascript caller. 如果是其他主机,则从您所说的配置文件(服务器上的配置文件在哪里?)中,您必须以某种方式将其发送给javascript调用程序。 One method is to attach it to a data- attribute, maybe on the button or a parent div or even the body. 一种方法是将其附加到data- attribute属性,该属性可以在按钮,父div甚至主体上。
<div id="thing_container" data-api-host="$configured_host">
Using $var or ${var} or whatever your server-side stack uses. 使用$var${var}或您的服务器端堆栈使用的任何内容。

You can get that with something like 你可以用类似的东西

var elem = document.getElementById('thing_container');
var api_host = elem.getAttribute('data-api-host');
saveEdit( api_host, myApiKey );

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

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