最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - jquery ajax url not including server path - Stack Overflow

matteradmin9PV0评论

I'm sorry guys but this is making me crazy. This is something I've done before, but for some reason it doesn't work.

I have a html button that fires a js function when clicked and passes a parameter:

<button type="button" class="btn btn-default" onclick="aprobarOperacion(Operacion.value)" data-dismiss="modal">

Next, my js function:

function aprobarOperacion(numeroOperacion) {

    var serviceUrl = "/Operaciones/AutorizarOperacion";

    $.ajax({
        type: "POST",
        dataType: "json",
        url: (window.BASE_URL == null) ? serviceUrl : window.BASE_URL + serviceUrl,
        data: JSON.stringify({
            operacion: numeroOperacion
        }),
        success: function (data) {
        //some code
        },
        error: function (data) {
        //some code
        },
    });
}

The thing is, this ajax function should go to Operaciones controller, and execute an action named AutorizarOperacion which expects a parameter named operacion. The URL should be like http://localhost:port/Operaciones/AutorizarOperacion, but instead the debugger console throws the following error:

Failed to load resource: net::ERR_NAME_NOT_RESOLVED --> http://operaciones/AutorizarOperacion

I dont't why but clearly the path is missing the server part. I've tried sereral ways to write the url but they all render like that.

Thanks a lot.

I'm sorry guys but this is making me crazy. This is something I've done before, but for some reason it doesn't work.

I have a html button that fires a js function when clicked and passes a parameter:

<button type="button" class="btn btn-default" onclick="aprobarOperacion(Operacion.value)" data-dismiss="modal">

Next, my js function:

function aprobarOperacion(numeroOperacion) {

    var serviceUrl = "/Operaciones/AutorizarOperacion";

    $.ajax({
        type: "POST",
        dataType: "json",
        url: (window.BASE_URL == null) ? serviceUrl : window.BASE_URL + serviceUrl,
        data: JSON.stringify({
            operacion: numeroOperacion
        }),
        success: function (data) {
        //some code
        },
        error: function (data) {
        //some code
        },
    });
}

The thing is, this ajax function should go to Operaciones controller, and execute an action named AutorizarOperacion which expects a parameter named operacion. The URL should be like http://localhost:port/Operaciones/AutorizarOperacion, but instead the debugger console throws the following error:

Failed to load resource: net::ERR_NAME_NOT_RESOLVED --> http://operaciones/AutorizarOperacion

I dont't why but clearly the path is missing the server part. I've tried sereral ways to write the url but they all render like that.

Thanks a lot.

Share Improve this question asked May 10, 2016 at 20:43 Rambo3Rambo3 3911 gold badge4 silver badges18 bronze badges 1
  • 3 The window has no BASE_URL property, so unless you're defining that elsewhere in your code it's not going to work. – Rory McCrossan Commented May 10, 2016 at 21:00
Add a ment  | 

3 Answers 3

Reset to default 3

Well as I understand you need protocol, hostname, and port.

So you should get it like this:

var baseUrl = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port: '');

And then you can use it in your script like:

url: baseUrl + serviceUrl,

You can use document.location

That object has the properties protocol, hostname, and port

function aprobarOperacion(numeroOperacion) {
    var baseUrl = window.location.protocol + "//" + window.location.hostname + (window.location.port ? ':' + window.location.port : '';
    var serviceUrl = "/Operaciones/AutorizarOperacion";

    $.ajax({
        type: "POST",
        dataType: "json",
        url: (window.BASE_URL == null) ? serviceUrl : window.BASE_URL + serviceUrl,
        data: JSON.stringify({
            operacion: numeroOperacion
        }),
        success: function (data) {
        //some code
        },
        error: function (data) {
        //some code
        },
    });
}

BONUS: If you happen to be using babel/es6 you can make things prettier like this, I love using template strings for concatenation.

const {protocol, hostname} = document.location;
const port = document.location.port ? `:${document.location.port}` : '';
const serviceUrl = '/Operaciones/AutorizarOperacion';
const url = `${protocol}//${hostname}${port}${serviceUrl}`;
var serviceUrl = window.location.href + "Operaciones/AutorizarOperacion";  // Raw javascript
var serviceUrl = $(location).attr('href') + "Operaciones/AutorizarOperacion";  // JQuery solution
Post a comment

comment list (0)

  1. No comments so far