2012-01-18 31 views
19

Tôi đang gặp sự cố khi sử dụng jQuery ajax của mình để hoạt động bình thường. Nó hướng đến trang PHP để cập nhật cơ sở dữ liệu, nhưng không bao giờ quay trở lại kịch bản cho các tùy chọn thành công hoặc lỗi.Lỗi thành công và lỗi chức năng Ajax

Mã của tôi là dưới đây:

$(document).ready(function(){ 
     $("form#updatejob").submit(function() { 
      function textreplace(x) {return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");} 
      // we want to store the values from the form input box, then send via ajax below 
      var job  = $("#job").attr("value"); 
      var description  = $("#description").val(); 
      description.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
      var startDate = $("#startDate").attr("value"); 
      var releaseDate = $("#releaseDate").attr("value"); 
      var status = $("#status").attr("value"); 
      $.ajax({ 
       beforeSend:textreplace(description), 
       type: "POST", 
       url: "updatedjob.php", 
       data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, 
       success: function(){ 
        $("form#updatejob").hide(function(){$("div.success").fadeIn();}); 
       }, 
       error: function(XMLHttpRequest, textStatus, errorThrown) { 
        alert("Status: " + textStatus); alert("Error: " + errorThrown); 
       }  
      }); 
      return false; 
     }); 
}); 

Và PHP:

<?php 
    include("connect.php"); 
    $job = trim($_POST['job']); 
    $startDate = trim($_POST['startDate']); 
    $releaseDate = trim($_POST['releaseDate']); 
    $mysqlstartdate = date('Y-m-d', strtotime($startDate)); 
    $mysqlreleasedate = date('Y-m-d', strtotime($releaseDate)); 
    $description = trim($_POST['description']); 
    $status = trim($_POST['status']); 
    $update = "UPDATE jobs SET startDate = '$mysqlstartdate', releaseDate = '$mysqlreleasedate', description = '$description', status = '$status' WHERE jobID = '$job' "; 
    $rsUpdate = mysql_query($update); 
// or die(mysql_error()); mysql_close(); 
?> 
+0

Điều gì sẽ xảy ra nếu bạn đặt 'alert()' trong dòng đầu tiên của hàm gọi lại 'thành công'? 'success: function() {alert ('foobar'); ...' – Jasper

+0

Có vẻ logic bạn cũng cung cấp mã php. Bạn đang lặp lại một phản ứng? –

+0

Đây là PHP: '' – michael

Trả lời

48

Hãy thử điều này:

$.ajax({ 
    beforeSend: function() { textreplace(description); }, 
    type: "POST", 
    url: "updatedjob.php", 
    data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, 
    success: function(){ 
     $("form#updatejob").hide(function(){$("div.success").fadeIn();}); 
    }, 
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
     alert("Status: " + textStatus); alert("Error: " + errorThrown); 
    }  
}); 

Thuộc tính beforeSend được thiết lập để function() { textreplace(description); } thay vì textreplace(description). Thuộc tính beforeSend cần một hàm.

+0

Tôi đã thay đổi phần đó, nhưng nó vẫn không trả về hàm. Cảm ơn bạn đã thử. – michael

0

Điều này có thể không giải quyết được tất cả các vấn đề của bạn, nhưng biến bạn đang sử dụng bên trong hàm (văn bản) của bạn không giống như tham số bạn chuyển vào (x).

Thay đổi:

function textreplace(x) { 
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
} 

Để:

function textreplace(text) { 
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); 
} 

vẻ như nó sẽ làm một số tốt.

3

Bạn có thể thực hiện logic lỗi cụ thể như sau:

error: function (XMLHttpRequest, textStatus, errorThrown) { 
    if (textStatus == 'Unauthorized') { 
     alert('custom message. Error: ' + errorThrown); 
    } else { 
     alert('custom message. Error: ' + errorThrown); 
    } 
} 
2

Đây có thể là một bài cũ nhưng tôi nhận ra không có gì để được trả lại từ php là gì và chức năng thành công của bạn không có đầu vào như như sau , success:function(e){}. Tôi hy vọng rằng sẽ giúp bạn.

4

Người ta cũng có thể sử dụng sau đây để bắt lỗi:

$.ajax({ 
    url: url, 
    success: function (data) { 
     // Handle success here 
     $('#editor-content-container').html(data); 
     $('#editor-container').modal('show'); 
    }, 
    cache: false 
}).fail(function (jqXHR, textStatus, error) { 
    // Handle error here 
    $('#editor-content-container').html(jqXHR.responseText); 
    $('#editor-container').modal('show'); 
}); 
3

Tôi đã có cùng một vấn đề và cố định nó bằng cách thêm một datatype = "text" dòng để gọi ajax của tôi. Làm cho dataType khớp với phản hồi bạn mong đợi để lấy lại từ máy chủ (thông báo lỗi "chèn thành công" hoặc "đã xảy ra lỗi").

0

Bạn đang gửi loại bài đăng có dữ liệu được triển khai để nhận. biểu mẫu của bạn phải như sau:

$.ajax({ 
url: url, 
method: "POST", 
data: {data1:"data1",data2:"data2"}, 
... 
Các vấn đề liên quan