Format of url field in an AJAX call using jQuery

时间:2016-08-23 15:35:18

标签: javascript php jquery ajax

I am trying to make an AJAX request via jQuery to a PHP script present in a different server (say the IP is 11.1.35.12) and the location of PHP script is "C:\inetpub\wwwroot\Kibana\mytelemetry.php" inside the server. What should be the url in my below ajax code?

$.ajax({

    url     :   "11.1.35.12/Kibana/mytelemetry.php",
    cache   :   false,
    data    :   ({
                    DshBrdName  : strFullDashBoardName,
                    DshBrdID    : currentDashboard, 
                    r           : Math.random()
                }),
    success :   function(data, textStatus, jQxhr){
                    //alert(textStatus); 
                },
    error   :   function(jqXHR, textStatus, errorThrown){
                    //alert(textStatus);
                    alert(errorThrown);
                },
    type    :   "POST"
});

P.S : The above is not working! I am pretty sure my url format is wrong.

1 个答案:

答案 0 :(得分:2)

You're missing the http protocol when passing in url and your script is requesting url in the current domain you're in.

try this:

$.ajax({

    url     :   "http://11.1.35.12/Kibana/mytelemetry.php",
    cache   :   false,
    data    :   ({
                    DshBrdName  : strFullDashBoardName,
                    DshBrdID    : currentDashboard, 
                    r           : Math.random()
                }),
    success :   function(data, textStatus, jQxhr){
                    //alert(textStatus); 
                },
    error   :   function(jqXHR, textStatus, errorThrown){
                    //alert(textStatus);
                    alert(errorThrown);
                },
    type    :   "POST"
});

Beside that, you have to add Access-Control-Allow-Origin header in your php script