我正在尝试在WCF中使用“POST”方法,但是我不能使用它,我在WCF服务中唯一可以使用的是“GET”方法,但是当我尝试使用“POST”时问题就开始了“发送对象的方法:
这是您的合同
namespace Mywcf
{
[DataContract]
public class Person
{
[DataMember]
public string Name { get; set; }
}
}
我的对象:
namespace Mywcf
{
public class Implementacion : Itest
{
public string Hello()
{
return "Hi";
}
public void AddObject(Person p)
{
string Name = p.Name;
TurnBack(Name);
}
public string TurnBack(string Name)
{
return Name;
}
}
}
我的实施:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="scripts/jquery-3.2.1.js"></script>
<!--This SCRIPT Use my WCF as a 'GET'Method and works really good-->
<script>
$(document).ready(function() {
$("#btnHi").click(function() {
Hello();
});
});
function Hello() {
$.ajax({
url: "http://localhost:83/Mywcf/Service.svc/Hello",
dataType: "json",
data: "{}",
type: "GET",
contentType: "application/json; utf-8",
success: function(msg) {
try {
alert(msg);
} catch (Exception) {}
},
error: function(result) {
alert("Error " + result.status + '' + result.statusText);
}
});
}
</script>
<!--This SCRIPT has many issues (404 BAD REQUEST )I use the WCF as a 'POST' Method-->
<script>
$(document).ready(function() {
$("#btnConfirm").click(function() {
var name = $('#txtTest').val();
Insert(name);
});
});
function Insert(name) {
var objectPerson = {
"Name": name
};
var stringJ = JSON.stringify(objectPerson);
$.ajax({
url: "http://localhost:83/Mywcf/Service.svc/AddObject",
dataType: "json",
data: "{'p':" + stringJ + "}",
type: "POST",
contentType: "application/json; utf-8",
success: function(msg) {
try {
alert(msg);
} catch (Exception) {
alert(Exception);
}
},
error: function(result) {
alert("Error " + result.status + '' + result.statusText);
}
});
}
</script>
<title>TESTING WCF</title>
</head>
<body>
<input type="button" id="btnHi" value="Hello" /> <br />
<input type="text" id="txtTest" placeholder="Tip a Name" />
<input type="button" id="btnConfirm" value="Ok" />
</body>
</html>
我的.ASPX(包括我的脚本):
{{1}}
我认为问题出现在合同内的标题中或者当我调用AJAX时。
答案 0 :(得分:0)
我认为这是附加json数据的错误方法。
data: "{'p':" + stringJ + "}",
忽略p作为传递它或以正确的方式连接json stringify。
function Insert(name) {
var objectPerson = {
"Name": name
};
// var stringJ = JSON.stringify(objectPerson);
$.ajax({
url: "http://localhost:83/Mywcf/Service.svc/AddObject",
dataType: "json",
data: JSON.stringify(objectPerson),
type: "POST",
contentType: "application/json; utf-8",
success: function(msg) {
try {
alert(msg);
} catch (Exception) {
alert(Exception);
}
},
error: function(result) {
alert("Error " + result.status + '' + result.statusText);
}
});
}
答案 1 :(得分:0)
@Hameed 是的!,我把它放在这里:
debugger;
$.ajax({
url: "http://localhost:83/WCFPruebaServicio/Servicio.svc/AgregarObjeto",
data: JSON.stringify(objetoPersona),
async: false,
crossDomain: true,
dataType: "json",
type: "POST",
contentType: "application/json; utf-8",
success: function (msg) {
try {
alert('Si se pudo :) '+msg);
} catch (Exception) {
alert(Exception);
}
},
error: function (result) {
alert("Error " + result.status + '' + result.statusText);
}
});