2012-10-06 116 views
-1

可能重複:
Detecting Ajax in PHP and making sure request was from my own website如何確保使用ajax?

我有一個表格上傳圖片,我使用Ajaxform()

$('#uploadformimage').ajaxForm({ 

    beforeSend: function() { 
     $(".progress").css("display","block"); 
     $("#response").html(""); 
     status.empty(); 
     var percentVal = '0%'; 
     bar.width(percentVal) 
     percent.html(percentVal); 
    }, 
    uploadProgress: function(event, position, total, percentComplete) { 
     var percentVal = percentComplete + '%'; 
     bar.width(percentVal) 
     percent.html(percentVal); 
     //console.log(percentVal, position, total); 
    }, 
    complete: function(xhr) { 
      $(".bar").css("width","100%"); 
      $(".percent").html("100%").delay("8000").parent().slideUp(1000); 
      $("#response").html(xhr.responseText); 

    } 
}); 

這是我的javascript代碼。我想搞清楚server-side表單是否由ajax上傳? Javascript可能在某些計算機上被禁用,因此我想顯示一些不同的數據?

+0

在問你的問題之前,請使用搜索。我只是在搜索中粘貼問題標題:http://stackoverflow.com/search?q=%5Bphp%5D+How+to+make+sure+ajax+is+used+or+not%3F - 請參閱http: //hackoverflow.com/questions/ask-advice – hakre

+0

@hakre它不是每個人都知道的一切。你應該看到這個[User hakre](http://stackoverflow.com/questions/6562276/how-to-merge-兩個數組 - 由僅接受數值 - 第二陣列 - 那 - 哈) – StaticVariable

+1

這是沒有藉口不使用搜索。 – hakre

回答

2
if ($_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') 
{ 
    // This is an AJAX request 
} 
+0

感謝它的完美工作? – StaticVariable

0

你可以將一個選項傳遞GET /foo?xhr=1

然後在PHP $_GET['xhr']

1

我建了一個演示,這裏的結果

Request URL:http://localhost/test.php 
Request Method:POST 
Status Code:200 OK 
Request Headersview source 
Accept:*/* 
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3 
Accept-Encoding:gzip,deflate,sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Content-Length:32 
Content-Type:application/x-www-form-urlencoded; charset=UTF-8 
Host:localhost 
Origin:http://localhost 
Referer:http://localhost/ 
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.11 (KHTML, like Gecko) Ubuntu/12.04 Chromium/20.0.1132.47 Chrome/20.0.1132.47 Safari/536.11 
X-Requested-With:XMLHttpRequest 
+0

我使用谷歌色調督察那裏你可以看到阿賈克斯呼叫以及每一個其他外部電話那裏形成客戶端立場點無論如何希望幫助 –

0

讓我們假設AJAX不參與。

所有的Javascript/AJAX都可以實現的是讓用戶體驗比競爭對手更好。

那麼,爲什麼你會關心圖像,當它由人來上傳時。該人可能沒有Javascript,

只要去最低限度,並添加花裏胡哨!

+0

我只是想顯示image.uploaded用戶。在正常情況下,我呼應img src。 – StaticVariable

0

您可以在HTML中使用<noscript>標籤來生成不同的HTML,因此表單可以發佈到不同的服務器端腳本或向表單添加隱藏字段。