【问题标题】:ASP.NET Core and JavaScript Ajax API Call to another hostASP.NET Core 和 JavaScript Ajax API 调用另一个主机
【发布时间】:2021-10-09 15:01:13
【问题描述】:

在 JavaScript ajax API 调用中处理实时和开发主机 URL 的最佳方法是什么?

例如,如果开发主机是https://hostdev.com/do-something,而直播是https://hostlive.com/do-something,我如何让它知道要使用哪个主机?

$.ajax({
    type: 'post',
    url: 'https://hostdev.com/do-something',
    dataType: "json",
    data: data,
    contentType: 'application/json',
    context: this,
    success: function (data) {
        ...
    },
    error: function (result) {
        ...
});

【问题讨论】:

  • 你可以在布局中设置一个全局变量来定义host的值,然后在你所有的ajax方法中,将url设置为variable+"/do-something"。

标签: javascript asp.net ajax asp.net-core


【解决方案1】:

如果您的主机是hostdev.com
它将发送到hostdev.com/do-something
否则发送至hostlive.com/do-something

const now_host = location.hostname
const send_to_host = now_host === "hostdev.com"?"hostdev.com":"hostlive.com"

$.ajax({
    type: 'post',
    url: send_to_host +  '/do-something',
    dataType: "json",
    data: data,
    contentType: 'application/json',
    context: this,
    success: function (data) {
        ...
    },
    error: function (result) {
        ...
 })

【讨论】:

    猜你喜欢
    • 2018-03-15
    • 1970-01-01
    • 2012-09-05
    • 2017-01-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-30
    • 2022-01-05
    • 1970-01-01
    相关资源
    最近更新 更多