2016-08-23 44 views
1

我想通过jQuery向不同服务器中存在的PHP脚本(比如IP为11.1.35.12)发出AJAX请求,并且PHP脚本的位置是“C :\ inetpub \ wwwroot \ Kibana \ mytelemetry.php“。我的下面的ajax代码应该是什么网址?使用jQuery的AJAX调用中的url字段的格式

$.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:以上不行!我很确定我的网址格式是错误的。

+1

https://en.wikipedia.org/wiki/Cross-origin_resource_sharing – RiggsFolly

回答

2

在URL传球和你的脚本请求的URL在你所在的当前域中,当你缺少http协议

试试这个:

$.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" 
}); 

在那旁边,你必须在您的php脚本中添加Access-Control-Allow-Origin标题

+1

您能否让我知道我需要添加到我的PHP脚本中的标题? –

+0

最简单的方法是在你的脚本中添加一行'header('Access-Control-Allow-Origin:*');''。最好在开始时,因为你不能在发送响应之后设置标题(比如'echo'或'var_dump()')。此外,你可以(也可能应该)用你的域名(无协议)替换'*'标志。 [MDN](https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS)上有很好的解读。 – Skysplit