Για να σου δώσω καλύτερα να καταλάβεις παραθέτω τον κώδικα (με μικρές δοαφορές απ' ότι δίνεται στο παλιό thread)
HTML Client:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
<script type="text/javascript">
function CallWcfAjax()
{
var xmlHttp = new ActiveXObject("Microsoft.XmlHttp");
var url = "Service1.svc/ajaxEndpoint/";
url = url + "Sum2Integers";
var newResult = "";
var body = '{"n1":';
body = body + document.getElementById("num1").value +',"n2":';
body = body + document.getElementById("num2").value + '}';
// Send the HTTP request
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/json");
xmlHttp.send(body);
// Create result handler
xmlHttp.onreadystatechange = function X() {
if (xmlHttp.readyState == 4) {
var resultsInRaw = xmlHttp.responseText;
document.getElementById("results").innerHTML = resultsInRaw;
}
}
}
</script>
</head>
<body>
First Value:
<input type="text" id="num1" /> <br>
Second Value:
<input type="text" id="num2" /> <br>
<input type="button" onclick="CallWcfAjax()" value="Call WCF via AJAX" />
<div id ="results"> </div>
</body>
</html>
_________________________________________________________________________________________________________________
WCF Service:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace Wcf2Ajax
{
// NOTE: If you change the class name "Service1" here, you must also update the reference to "Service1" in Web.config and in the associated .svc file.
public class Service1 : IService1
{
public string Sum2Integers(int n1, int n2)
{
int result = n1 + n2;
string resultStr = result.ToString();
// set up the myConnection
SqlConnection myConn = new SqlConnection("Data Source=CEID-3A863D11D4;Initial Catalog=SomeTable;Integrated Security=True");
SqlCommand cmd = new SqlCommand("INSERT INTO SomeTable values(@firstvalue,@secondvalue)", myConn);
cmd.Parameters.Add("@firstvalue", SqlDbType.TinyInt).Value = n1;
cmd.Parameters.Add("@secondvalue", SqlDbType.TinyInt).Value = n2;
cmd.CommandTimeout = 45;
cmd.ResetCommandTimeout();
//CommandTimeout is now reset to 30
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
return (resultStr);
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
Αν και το Service επιστρέφει κανονικά το αποτέλεσμα όταν δεν προσπαθώ να κάνω το insert, όταν προσπαθώ να κάνω insert μου πετάει το μήνυμα:
Cannot open database "SomeTable" requested by the login. The login failed.
Login failed for user 'CEID-3A863D11D4\user'.
Μήπως χρειάζεται να προσθέσω κάποια παράμετρο στο web.config όπως :
<appSettings>
<add key="Data Source=CEID-3A863D11D4;Initial Catalog=SomeTable;Integrated Security=True" value="Server=CEID-3A863D11D4; Initial Catalog = SomeTable;uid = sa;pwd=sa"/>
</appSettings>
Ελπίζω να έγινα κατανοητός για το τι θέλω να φτιάξω.