簡體   English   中英

跨域JavaScript Ajax

[英]Cross domain javascript ajax

我已經整整一個星期都在解決問題,在任何地方都找不到答案。

這就是問題:

XMLHttpRequest無法加載http://www.websiteA.com/process.php Access-Control-Allow-Origin不允許使用來源http://clientwebsi.te

我有一個Javascript文件,該文件位於網站A的服務器上。客戶端可以在該網站上加載JS文件。

在服務器A上,還存在一個process.php,它將信息放入服務器A的數據庫中。

我現在正在使用此代碼:

var dataText = 'page=' + top.location.host;
$.ajax({
    type: "POST",                 
    url: "process.php",         
    data: dataText,              
    error: function(request,error){
            alert(error);
        },
    success: function(request) {
        alert(request.length);
    }
});

此代碼在localhost上完美運行,但在我使用服務器A和客戶端服務器(跨域)時無效

這是在線代碼:

$.ajax({
    type: "POST",                 
    url: "http://www.serverA.com/process.php",         
    dataType: "json",
    data: dataText,              
    error: function(request,error){
            alert(error);
        },
    success: function(request) {
        alert(request.length);
    }
});
$.ajax({
    type: "POST",                 
    url: "http://www.serverA.com/process.php",         
    dataType: "jsonp",
    data: data,  
    crossDomain: true,            
    error: function(request,error){
            alert(error);
        },
    success: function(request) {
        alert(request.length);
    }

唯一的方法是發出jsonp GET請求。 這很容易,但是您不能使用其他請求類型。

$.ajax({
    dataType: 'jsonp',
    url: 'http://domain.de/jsonp.php',
    success: function(data, textStatus, jqXHR),
    error: function(jqXHR, textStatus, errorThrown)
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM