0

I am trying to consume WCF service in a PhoneGap application for Android using jquery ajax which is hosted in a intranet domain.

As a response I am getting message based on my ajax request in the below.

Ajax Request:

    $.ajax({ 

        url: "http://myIP/wcfService.svc/myFun",
        type: "POST",  

        data: JSON.stringify(req),
        dataType: "json",

        success: OnSuccess,

        error: function(xhr,err){
            alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
            alert("responseText: "+xhr.responseText);
        }
    });

Request Header

 Accept   application/json, text/javascript, */*; q=0.01
 Accept-Encoding  gzip, deflate
 Accept-Language  en-US,en;q=0.5
 Cache-Control    no-cache
 Connection   keep-alive
 Content-Length   39
 Content-Type application/x-www-form-urlencoded; charset=UTF-8
 DNT  1
 Host MyIP
 Origin   null
 Pragma   no-cache
 User-Agent   Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Firefox/38.0

Response Header

HTTP/1.1 415 Cannot process the message because the content type 'application/x-www-form-urlencoded; charset=UTF-8' was not the expected type 'text/xml; charset=utf-8'.

Cache-Control: private

Server: Microsoft-IIS/8.5

X-AspNet-Version: 4.0.30319

X-Powered-By: ASP.NET

Access-Control-Allow-Origin: *

Access-Control-Allow-Methods: GET, PUT, POST, DELETE, HEAD, OPTIONS

Access-Control-Allow-Credentials: true

Access-Control-Allow-Headers: X-Requested-With, origin, content-type, accept

Date: Fri, 15 Jul 2016 16:26:25 GMT

Content-Length: 0

Error Status:

415 Cannot process the message because the content type 'application/x-www-form-urlencoded; charset=UTF-8' was not the expected type 'text/xml; char

Nemo
  • 352
  • 3
  • 13
  • Take a look here: http://stackoverflow.com/questions/14403492/cannot-process-the-message-because-the-content-type-application-json-charset-u – Frix33 Jul 18 '16 at 10:13
  • Already check with the solution.. but not worked for me... Anyways thanks.. – Nemo Jul 18 '16 at 10:46
  • Mhh but it looks like your service endpoint cant accept webform data.. Please post your binding config, or check messageEncoding property on binding. – Frix33 Jul 18 '16 at 11:02
  • 1
    <%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.YourServiceClass" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %> – tnt-rox Jul 18 '16 at 11:03
  • @Nemo I suggest you to look at the last comment in this link - http://stackoverflow.com/questions/26625191/http-415-cannot-process-the-message-because-the-content-type-application-json It says use webHttpBinding instead of basicHttpBinding for JSON Hope it may help – Gandhi Jul 18 '16 at 11:57
  • @gandhi, tried your given solution but the service is not accessible.. – Nemo Jul 21 '16 at 12:56

1 Answers1

1

Try below code

$.ajax({ 

    url: "http://myIP/wcfService.svc/myFun",
    type: "POST",  

    data: JSON.stringify(req),
    dataType: "json",
    contentType: "application/json",

    success: OnSuccess,

    error: function(xhr,err){
        alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
        alert("responseText: "+xhr.responseText);
    }
});
Vivek
  • 11
  • 2