0
var addRecord, getRecords, wcfServiceUrl;

// Use strict for all other functions
// Based on post at:
// http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/

(function () {
    "use strict";

    wcfServiceUrl = "http://localhost/WcfRestUpdates/eval/evals";

    // Execute when the DOM is fully loaded
    $(document).ready(function () {
        getRecords();
    });

     // Add record
    $(function () {
        $("#butCallAjax").click(addRecord);                

    });     

    //Add record function start
    addRecord = function () {
        //jQuery.support.cors = true;
        $.ajax({
                    type: "POST",
                    url: wcfServiceUrl,                    
                    //data: '<Eval xmlns="http://schemas.datacontract.org/2004/07/WcfRestUpdates"><Comments>' + document.getElementById("txtComment").value + '</Comments><Submitter>' + document.getElementById("txtSubmitter").value + '</Submitter></Eval>',
                    data: '<Eval xmlns="http://schemas.datacontract.org/2004/07/WcfRestUpdates"><Comments>' + document.getElementById("txtComment").value.toString() + '</Comments><Submitter>' + document.getElementById("txtSubmitter").value.toString() + '</Submitter></Eval>',
                    //data: jQuery.parseJSON('{"Comments":"This is test comment","Id":"1","Submitter":"Mayank","Timesent":"\/Date(928129800000+0530)\/"}'),
                    //ContentType:"application/json; charset=utf-8",
                    //dataType: "json",   
                    contentType: "application/xml",                   
                    dataType: "xml",   
                    success: getRecords,
                    error: function (xhr) {
                        if(xhr.status = 200) {
                            getRecords;
                        }
                        else{
                            alert("Error in POST:" + xhr.status.toString() + "and "+ xhr.responseText);                        
                            //var obj = jQuery.parseJSON('{"Comments":"This is test comment","Id":"1","Submitter":"Mayank","Timesent":"\/Date(928129800000+0530)\/"}');
                            //alert("Error in POST:");  
                            //alert("Error in POST:" + obj.Id);                        
                        }
                    }
                });        
    };

    //Add record function end

    //Get records function start
    getRecords = function() {
        jQuery.support.cors = true;
        $.ajax({
            type: "GET",
            url: wcfServiceUrl,
            data: "{}",            
            ContentType:"application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {

                //alert('success');
                //var resultText = "<div>";
                var divR = document.getElementById("divResult");
                //[{ "Comments": "this is my first comment", "Id": "1", "Submitter": "Mayank", "Timesent": "\/Date(1329473557750+0530)\/" }, { "Comments": "this is my first comment", "Id": "2", "Submitter": "Mayank", "Timesent": "\/Date(1329473813781+0530)\/"}]
                divR.innerHTML = '<div style="clear:both;width:7%;float:left;padding:5px;" class="sb">' + 'Id' + '</div>';
                divR.innerHTML += '<div style="width:15%;float:left;padding:5px;" class="sb">' + 'Submitter' + '</div>';
                divR.innerHTML += '<div style="width:40%;float:left;padding:5px;" class="sb">' + 'Comments' + '</div>';
                divR.innerHTML += '<div style="width:20%;float:left;padding:5px;" class="sb">' + 'Date/Time' + '</div>';
                $.each(data, function (i, theItem) {

                    //var option = document.createElement("option");
                    //option.text = theItem.toString();
                    //option.value = theItem.toString();

                    try {
                        //alert('success add combo');
                        //combo.add(option, null); // Other browsers
                        var datetime = new Date(theItem.Timesent.match(/\d+/)[0] * 1);
                        divR.innerHTML += '<div style="clear:both;width:7%;float:left;padding:5px;" >' + theItem.Id.toString() + '</div>';
                        divR.innerHTML += '<div style="width:15%;float:left;padding:5px;" >' + theItem.Submitter.toString() + '</div>';
                        divR.innerHTML += '<div style="width:50%;float:left;padding:5px;" >' + theItem.Comments.toString() + '</div>';
                        divR.innerHTML += '<div style="width:20%;float:left;padding:5px;" >' + datetime.toDateString() + '</div>';
                    }
                    catch (error) {
                        alert('error found');
                        combo.add(option); // really old browser
                    }

                });
            },
            error: function (xhr) {
                        alert("Error in GET:" + xhr.responseText);
            }               
        });
    };
    //Get records function end
}) ();

一旦 POST 成功提交,我需要向 div 显示结果。它适用于 FireFox。但对于 Safari,我需要刷新浏览器以获取 POSTED 数据。“getRecords”函数用于在 div 中显示数据。在页面加载的所有浏览器中都可以正常工作。但它不能作为 ajax POST > 成功的一部分工作。

4

1 回答 1

1
$.ajax({
                type: "POST",
                url: wcfServiceUrl,
                //data: '<Eval xmlns="http://schemas.datacontract.org/2004/07/WcfRestUpdates"><Comments>' + document.getElementById("txtComment").value + '</Comments><Submitter>' + document.getElementById("txtSubmitter").value + '</Submitter></Eval>',
                data: '<Eval xmlns="http://schemas.datacontract.org/2004/07/WcfRestUpdates"><Comments>' + document.getElementById("comment").value + '</Comments><Submitter>' + document.getElementById("submitter").value + '</Submitter></Eval>',
                contentType: "text/xml",
                dataType: "xml",
                success: function (xhr) { getRecords(); },
                error: function (xhr) {
                            if(xhr.status = 200) {                            
                                getRecords();
                            }
                            else{
                                alert("Error while adding record with error-code:" + xhr.status.toString() + "and "+ xhr.responseText);                                                   
                            }
                        }

            });

ajax“POST”成功中函数“getRecord”的不正确调用。使用“function (xhr) { getRecords(); }”而不是“getRecord”并且它有效!

于 2012-02-28T06:20:22.537 回答