简体   繁体   中英

ajax call to wcf service returning error

I am trying to use a WCF web service. The service is returning results in the browser in Json format but when I am trying to get the data through ajax call it is giving error. My code is as follows. The service is running on a public server and it is being used in angularjs. In angularjs the service is returning Json data. It is the first time I am using WCF svc service call.

var Type;
var Url;
var Data;
var ContentType;
var DataType;
var ProcessData;

function WCFJSON() {

Type = "GET";
Url = "http://server.com/main/webservices/econnect.svc/getdata";

ContentType = "application/json; charset=utf-8";
DataType = "json"; varProcessData = true; 
CallService();
}

function CallService() {
$.ajax({
    type: Type, //GET or POST or PUT or DELETE verb
    url: Url, // Location of the service

    contentType: ContentType, // content type sent to server
    dataType: DataType, //Expected data format from server
    processdata: ProcessData, //True or False
    success: function(msg) {//On Successfull service call
        ServiceSucceeded(msg);
    },
    error: ServiceFailed// When Service call fails
    });
}

function ServiceFailed(result) {
    alert('Service call failed: ' + result.status + '' + result.statusText);
    Type = null;
    varUrl = null;
    Data = null; 
    ContentType = null;
    DataType = null;
    ProcessData = null;
}
function ServiceSucceeded(result) {
    if (DataType == "json") {
        resultObject = result.GetUserResult;

    for (i = 0; i < resultObject.length; i++) {
        alert(resultObject[i]);
    }

    }

}

function ServiceFailed(xhr) {
    alert("Error:" + xhr.responseText);

    if (xhr.responseText) {
        var err = xhr.responseText;
        if (err)
            error(err);
        else
            error({ Message: "Unknown server error." })
    }

    return;
}

$(document).ready(
    function() {
        WCFJSON();
    }

I think, it's due to syntax error:

varProcessData=true;  

should be:

var ProcessData=true;

other way you could commented this line:

//processdata: ProcessData, //True or False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM