简体   繁体   中英

calling wcf service from html always goes to error function. Works fine with aspx page

I have created a simple webservice which is hosted in same domain.

Now, I have created two pages

1) Aspx page where I'll be calling that webservice and it works just fine. Code is pasted below:

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

First Name: Last Name:

    $(function () {

        $("#btnSayHello").click(function () {

            var txtFirstName = $("#MainContent_txtFirstName").val();
            var txtSecondName = $("#MainContent_txtLastName").val();
            $("#MainContent_lblResult").html("Hello " + txtFirstName + " " + txtSecondName);

            $.ajax({
                type: "GET",
                contentType: "json",
                datatTypes: "json",
                url: "http://10.24.104.196/TestServiceCore.svc/GetName/",
                success: function (data) {
                    alert(data);
                }
            });
        })
    });

</script>

This thing works just fine..

2) I've tried using same javascript code for an simple HTML page (code as shown below)

<!DOCTYPE html>

Employee Hub Employee Hub - A Basic Test Application

    </section>
</form>
<input id="btnSayHello" type="button" value="Get Value From WCF" /><br />
<label for="male" id="lblResult">Test</label>
<script type="text/javascript">
    $(function () {

        $("#btnSayHello").click(function () {
            $.ajax({
                type: "GET",
                contentType: "json",
                datatTypes: "json",
                url: "http://10.24.104.196/TestServiceCore.svc/GetName/",
                success: function (data) {
                    alert("Success");
                },
                error: function (result) {
                    //alert('error; ' + eval(result));
                    window.alert("eRROR");
                }
            });
        })
    });
</script>

for this case it always goes to error function.

Not sure what's going wrong here for error it only produces "object object"

Any help would be greatly appreciated.

Update : I am still having this problem... and the problem area for this issue is only in AJAX call. calling the URL otherwise works fine.

Any help will be greatly appreciated..

Added source code link to repo this issue.

There is typo error in the source code you provided ,rename datatTypes to datatType

i have checked the code from the link you provided and it is working fine.

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