Reputation: 1099
Im trying to send a REST request to a WCF Service with javascript. I have tried the service with a .net client and it works fine this is my example method in the wcf service:
[OperationContract]
[WebInvoke(Method="POST",
ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
bool GuardarJuan(Persona persona);
From javascript i have tried these methods:
function sendRequest3(){
var datos='{"IdPersona":2,"Nombre":"David BQ"}';
var parameters = JSON.stringify(datos);
var req=$.ajax({
url: 'http://localhost:8732/Design_Time_Addresses/RestTEST/GuardarJuan',
type: 'POST',
data: parameters,
contentType: 'application/json',
dataType: 'jsonp'
});
}
And also like this:
function sendPost(){
var url = "http://localhost:8732/Design_Time_Addresses/RestTEST/GuardarJuan";
var persona={
IdPersona: 2,
Nombre: 'David BQ'
};
var parameters = JSON.stringify(persona);
xmlHttp.open("POST", url, true);
xmlHttp.setRequestHeader("Content-type", "application/json");
xmlHttp.send(parameters);
xmlHttp.onreadystatechange= function X()
{
if(xmlHttp.readyState == 4)
{
alert(xmlHttp.responseText);
}
}
but none of this work.
It always give me a 405 error Method not allowed or 0x80004005 (NS ERROR FAILURE)
Thanks
EDIT: Problem solved.. It was the cross-domain problem. I was doing it with the VS Server, so I changed to IIS and everything worked fine
Upvotes: 0
Views: 2268
Reputation: 19334
First, is the "Service" on the same domain as the page you are attempting to talk to the service with?
Second, if you are attempting to use JSONP, you can only use "GET" not "POST" as JSONP simply creates a <script src="service...?callback=somerndmethod"> that wraps the response in somerndmethod(response) ... which executes your expected code.
You may need to use IIS/IISExpress and have both the main, and wcf application on the same host:port.
Upvotes: 1