scm0sml
asked on
using jquery inside a javascript function.
Hi,
I have the following javascript function which is inside an include file within my asp.net website.
function saveTrackingData()
{
//debug to make sure we are hitting this function, and we are!
alert("hey");
//Test call to get call to .net working
$.ajax({
type: "POST",
url: "~/Index.aspx/HelloWorld",
data: "{}",
contentType: "application/json",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
alert(msg);
}
});
What I am basically trying to do is within the javascript file use jquery to call my helloworld function just as proof of concept....
I'm fairly new to javascript so am not sure if this is along the right lines...
Can someone advise as this isn't working?
Thanks in advance.
I have the following javascript function which is inside an include file within my asp.net website.
function saveTrackingData()
{
//debug to make sure we are hitting this function, and we are!
alert("hey");
//Test call to get call to .net working
$.ajax({
type: "POST",
url: "~/Index.aspx/HelloWorld",
data: "{}",
contentType: "application/json",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
alert(msg);
}
});
What I am basically trying to do is within the javascript file use jquery to call my helloworld function just as proof of concept....
I'm fairly new to javascript so am not sure if this is along the right lines...
Can someone advise as this isn't working?
Thanks in advance.
Since the url you assigned is a relative path, make sure that the URI http://yourdomain/~/Index.aspx/HelloWorld exists.
ASKER
chaituu:I presume you are asking if the hello world function is on the current page?
Yes the page we are on is index.aspx...
And yes stringray it does exist....
<WebMethod(EnableSession:= False)> _
Public Shared Function HelloWorld() As String
Return "Hello: " & DateTime.Now.Millisecond
End Function
Yes the page we are on is index.aspx...
And yes stringray it does exist....
<WebMethod(EnableSession:=
Public Shared Function HelloWorld() As String
Return "Hello: " & DateTime.Now.Millisecond
End Function
removed data option.check it now;
$.ajax({
type: "POST",
url: "~/Index.aspx/HelloWorld",
contentType: "application/json",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
alert(msg);
}
error: function (msg) {
alert("error");
}
});
$.ajax({
type: "POST",
url: "~/Index.aspx/HelloWorld",
contentType: "application/json",
dataType: "json",
success: function (msg) {
// Replace the div's content with the page method's return.
alert(msg);
}
error: function (msg) {
alert("error");
}
});
use firebug net tab to see what your code is trying to access
I think you should remove the ~/ from the url to make it work
I think you should remove the ~/ from the url to make it work
ASKER
still no joy i'm afraid?!
ASKER
something to add....
index is at the root level... the js file which gets included is in a folder "js" which is in the root folder...
index is at the root level... the js file which gets included is in a folder "js" which is in the root folder...
Use this
url: "/Index.aspx/HelloWorld"
instead of
url: "~/Index.aspx/HelloWorld"
url: "/Index.aspx/HelloWorld"
instead of
url: "~/Index.aspx/HelloWorld"
this url: "~/Index.aspx/HelloWorld"
could be
url: "Index.aspx/HelloWorld",
I guess tilde(~) sign is not recognized by JS compiler
could be
url: "Index.aspx/HelloWorld",
I guess tilde(~) sign is not recognized by JS compiler
ASKER
still not working.....
Show your output from
http://your_domain_name.com/Index.aspx/HellowWorld
OR if it is in development server
then
http://localhost:yourport/Index.aspx/HellowWorld
http://your_domain_name.com/Index.aspx/HellowWorld
OR if it is in development server
then
http://localhost:yourport/Index.aspx/HellowWorld
Try this
<WebMethod(EnableSession:=False)> _
Public Shared Function HelloWorld() As String
Return "{Hello: " & DateTime.Now.Millisecond & "}"
End Function
ASKER
Still no joy.
I have stripped my code down to about two files and attached a zip file.
I have renamed the ext txt....
Hopefully this will allow you to have a play rather than guessing?
If you need any more info let me know?
testJquery.txt
I have stripped my code down to about two files and attached a zip file.
I have renamed the ext txt....
Hopefully this will allow you to have a play rather than guessing?
If you need any more info let me know?
testJquery.txt
what do you get from the net tab in firebug?
What if you get the page with this code
$.ajax({
type: "POST",
url: "~/Index.aspx/HelloWorld",
data: "{}",
contentType: "text/html",
dataType: "html",
success: function (msg) {
// Replace the div's content with the page method's return.
alert(msg);
}
});
Or try this
$.ajax({
type: "POST",
url: "Index.aspx/HelloWorld",
data: "{}",
contentType: "text/html",
dataType: "html",
success: function (msg) {
// Replace the div's content with the page method's return.
alert(msg);
}
});
ASKER
stringray your last post gave me a load of html back but I couldnt debug into the function in .net?
Is it worth you having a go of the project I gave?
Is it worth you having a go of the project I gave?
Since I'm not much familiar to .NET, I'm not if you have to set the returned content type as "text/javascript" or "application/json".
See this link for further detail.
http://stackoverflow.com/questions/894828/creating-a-json-header-on-asp-net
See this link for further detail.
http://stackoverflow.com/questions/894828/creating-a-json-header-on-asp-net
Yes, my last post queries for a html page instead of json page. This is my intention to check if your page returns what you expect. You have said it returns html code instead of json like {Hello:155351123}. This means there is something wrong with your page.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Check what you have received in your full website. You can use network tab in the firebug or Network tab in google chrome's developer tools to inspect these.
ASKER
I'm stuck on a council network at the moment that is blocking me from downloading firebug or chrome.....
Will have to have a look once i've managed to get access....
Will have to have a look once i've managed to get access....
You are using IE? which version? if 9, there is the similar feature in Network tab in Developer Tools in IE9.
ASKER
stuck with 7......
Flaming councils!!
Have got xp so can't upgrade
Flaming councils!!
Have got xp so can't upgrade
Ok. Can you tell me your full website address of "Index.aspx/HelloWorld"?
ASKER
uSING THIS i GOT IT TO WORK EVENTUALLY.
ASKER
Can we get this sorted so I can ask another question?
is this jax call is hitting this asp page.