Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am calling this method 'Get_myHours' from html page which is in the same project.
its not working please help !!!

JavaScript
function show() {
        $.ajax({
            type: "GET",
            url: "http://localhost:51065/Service1.svc/Get_myHours",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: {
                loginIDtxt: loginIDtxt
            },
            success: function (response) {
                var Days = response.Days;
                var FinalTime = response.FinalTime;
                $('#jsonData').html('');
                $('#jsonData').append('<table><tbody><tr><th>' +
                  'Days</th><th>FinalTime</th>' +
                  '</tr><tr><td>' + Days + '</td><td>' + FinalTime +
                '</td></tr></tbody></table>');
            },
            error: function (msg) {
                alert("error");
            }
        });
    }


<div>
        &lt;input id="loginIDtxt" type="text" />
        &lt;button onclick="show()">Enter&lt;/button>
    </div>
    <div id="jsonData">

    </div>

</PRE>
Posted
Updated 28-Sep-15 1:33am
v2
Comments
Palash Mondal_ 28-Sep-15 3:44am    
Are you getting any error or exception in browser console? Could you please explain what you mean by "not working" as it is not clear right now.
Palash Mondal_ 28-Sep-15 3:46am    
Put a console.log(response) inside the ajax success method. What are you getting in the console for it?? Also put an alert inside the show() function. Is it called in the first place??
[no name] 28-Sep-15 6:04am    
alert is coming.. but its not hitting Get_myHours method :(

1 solution

Use POST method instead of GET method because you are sending data to service. Secondly make your service running. Thirdly make sure you have one parameter loginIDtxtin Get_MyHours method. Fourthly you were sending loginIDtxtin as param meter value, it should be textbox value. See below code:
JavaScript
$.ajax({
	type: "POST",
	url: "Service1.svc/Get_myHours",
	contentType: "application/json; charset=utf-8",
	dataType: "json",
	data: '{loginIDtxt:"' + $("#loginIDtxt").val() + '"}',
	success: function (response) {
		var Days = response.Days;
		var FinalTime = response.FinalTime;
		$('#jsonData').html('');
		$('#jsonData').append('<table><tbody><tr><th>' +
		  'Days</th><th>FinalTime</th>' +
		  '</tr><tr><td>' + Days + '</td><td>' + FinalTime +
		'</td></tr></tbody></table>');
	},
	error: function () {
	   alert("error");
	}
});


Note: In browser, use developer tool to see where you are getting error by pressing F12 and go to network tab.
 
Share this answer
 
Comments
[no name] 28-Sep-15 6:07am    
I am getting error alert.
My html page is in same wcf service project so i have given the url in this way
http://localhost:51065/Service1.svc/Get_myHours
[no name] 28-Sep-15 6:31am    
As I told, open chrome browser and press F12. Then go to network tab and see what error you are getting from server.

Secondly provide WCF code how you are using it.
[no name] 28-Sep-15 7:10am    
This is the error :

http://localhost:51065/Service1.svc/Get_myHours Failed to load resource: the server responded with a status of 415
(Cannot process the message because the content type 'application/json; charset=UTF-8' was not the expected type 'text/xml; charset=utf-8'.)

jquery-1.7.1.js:8102

POST http://localhost:51065/Service1.svc/Get_myHours 415 (Cannot process the message because the content type
'application/json; charset=UTF-8' was not the expected type 'text/xml; charset=utf-8'.)


This is the method:

[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "Get_myHours", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public string Get_myHours(int loginID)
{
using (MyEntities OE = new MyEntities())
{
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(OE.GET_HRbyIDdate(loginID).ToList<get_hrbyiddate_result>());
return json;
}
}
[no name] 28-Sep-15 7:30am    
Try below code:

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "Get_myHours", ResponseFormat = WebMessageFormat.Json,
RequestFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
public string Get_myHours(string loginIDtxt)
{
using (MyEntities OE = new MyEntities())
{
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(OE.GET_HRbyIDdate(Convert.ToInt(loginIDtxt)).ToList());
return json;
}
}

And secondly use WebHttpBinding/wsHttpBinding. Please go through below link:

http://www.encodedna.com/wcf/tutorial/call-wcf-service-from-jquery-ajax-json-aspdotnet.htm

http://www.aspsnippets.com/Articles/Make-AJAX-JSON-call-to-ASP.Net-WCF-Service-using-jQuery-and-Javascript.aspx
[no name] 29-Sep-15 1:22am    
Its not working i changed the config file as with the able links :(

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900