how to call url in ajax json request - c#

i need to call webservice from a directory in my project but
url: "~/RA/WebServiceRAOpen.asmx/OpenedRAlistByBranch" but it is not working
$.ajax({
url: "~/RA/WebServiceRAOpen.asmx/OpenedRAlistByBranch",
data: "{ 'ranumber': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
})
hiow can i call the url from a directory

Try building up the url with the abolute server path, e.g. using window.location like this:
$.ajax({
url: window.location.host + "/RA/WebServiceRAOpen.asmx/OpenedRAlistByBranch"
...
A relastive url should also work. Just leave out the "~"-prefix. Note that you should execute JavaScript code not locally but from a real http server. Debugging in Visual Studio uses a local http server, so this will do.

Urls starting with ~/ are ASP.NET Urls. To use it in JavaScript you need to map it to actual Urls. Render the path on your page into a JavaScript variable and then use it in your script.
HttpContext.Current.Server.MapPath("~/RA/WebServiceRAOpen.asmx/OpenedRAlistByBranch") will get you the actual path. I have not used ASP.NET in a while, so I do not remember the correct asp:label syntax anymore to get you the full way.

Related

Consume ASP .Net WebService using HTML AJAX

I am trying to call a simple hellowworld function of ASP .NET WebService using JQuery AJAX call written in separate HTML file i.e. Not aspx file in ASP .NET.
Here is code of my AJAX call which works fine in aspx based web client.
$.ajax({
type: "POST",
url: "/Test1/RSSReader.asmx/GetRSSReader",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
$("#output").text(msg.d);
},
error: function (e) {
$("#output").html("WebSerivce error " + e.responseText);
}
});
I want to call same webservice and same method using .html file but it is returning error. I tried giving server address i.e. localhost but still no response.

Issue with Url routing - Asp.Net MVC Razor

I use "/Controller/ActionName" url format for my ajax calls. This pattern works fine with my development server that has this pattern
/Controller/ActionName
My application has been deployed to IIS with the following url pattern
/domain/Controller/ActionName
So whenever I call a resource it routes to /Controller/ActionName instead of /domain/Controller/ActionName there by resulting in 404 error. Is there any quick fix for this by modifying the routeconfiguration such that it works on both the devl as well as the IIS.
If you defining the relative urls "manually" as a string try not using a '/' in the beginning.
"Controller/ActionName"
Edit
What I've done the the past is included a Partial Razor View that is responsible of setting urls using #Url.Action()
Partial - Shared/JavascriptUrlsPartial.cshtml
<script type="text/javascript">
(function ($, app) {
app.urls =
{
actionA: '#Url.Action("Controller", "ActionA")',
};
})(jQuery, app = app || {});
</script>
Then you reference the rendered url in javascript.
$.ajax({
url: app.urls.actionA,
//etc
})
This partial is then included in the _Layout.cshtml.
In my Razor view, we have Ajax calls like:
var model = { searchId: searchId, newSearchName: searchName, newSearchDescription: description };
$.ajax({
url: '#Url.Action("Rename", "Search")',
contentType: 'application/json; charset=utf-8',
type: 'POST',
dataType: 'html',
data: JSON.stringify(model)
})
We have not done anything special in local or production environment. We host the application on IIS 7.5.
I could not make out what's different in your case from your question and comments. If this does not solve the problem please add more information around your setup.

How to get url of the caller page that initiated the ajax call

My web service wants to know what is the page url of the caller.
I checked the HttpReq -> Url it is the actual web service Url. Also the HttpReq -> UrlReferrer but it is not right, neither.
Is it possible to find out the caller page url from server side at all? Or do I have to pass in the url via service dto?
Thanks for your help :)
If, for whatever reason, UrlReferrer doesn't work out for you then... the page where the request came from knows what its address is, right? Why not supply this data to the javascript that served the Ajax request, so you can send it up with the Ajax request?
Pass the page URL as a parameter to your web service by using the JavaScript document.location.href notation, like this:
$.ajax({
type: "POST",
url: "YourPage.aspx/DoSomething",
data: "{'pageUrl' : window.location.href}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data) {
}
});
Then you can handle the pageUrl value in your service code.

unable to render Json string on client side

ascx.cs
protected string BindData()
{
List<Product> products = product.GetRepeaterData(prod);
string json = JsonConvert.SerializeObject(products);
return json;
}
ascx
<script type="text/javascript" language="javascript">
function doSomething() {
$.ajax({
type: "POST",
url: "/ProgramListSimple.aspx/BindData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
alert(msg);
}
});
};
I am not able to see any alert ..I dont know if the ajax function is doing what it is suppose to do..this code is for user control & not on the aspx page does that matter? while debugging I am able to see the serialized data in json string. Its just that its not rendering on the client side....working on it since morning now I need some help please..any examples or any doc can also be useful..
You have to use the d property
alert(msg.d);
If you are using Chrome or Firefox to debug use the following to inspect a JavaScript object:
console.log("%o", msg);
In Chrome press Ctrl + Shift + J to show the developer console
I created a new aspx page. Transfered all the code behind logic to the aspx.cs from ascx.cs. The just called the url of the aspx page from my ascx page using ajax callback
type: "GET",
url:'<%=VirtualPathUtility.ToAbsolute("~/ProgramListSimpledetail.aspx") %>',
data: dataObject,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data)
I just added a small part of the fix that is very important for this to work other then this there were bunch of things that were added to make the ascx page inherit the properties from the apsx page.... but I think that was mainly related to my code...so I hope this helps someone in future...thanks for all those who tried to contribute..

How to optimize multiple $.ajax({...}) function loading in Asp.net?

I calling multiple function on page load in my asp.net web application.
All function run on page ready method
see the below code
$(document).ready(function(){
func1();
func2();
func3();
//.... so on.
});
function func1()
{
$.ajax({
type: "POST",
url: "Contents.asmx/GetText",
data: "{ }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
/some code
}
});
}
function func2()
{
$.ajax({
type: "POST",
url: "Contents.asmx/GetTitles",
data: "{ }",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
/some code
}
});
}
.... like wise others
For backend I using SQL server-05.
When page is loading It getting more time to load,
above functions takes 30+ seconds to load.
How to optimze function calling?..
You could aggregate all the queries into a new web method which will perform all the SQL queries at once. Then send a single AJAX request to this new method. And as far as your SQL server is concerned you could also send multiple SQL queries into a single round-trip. Try optimizing the SQL queries as much as possible. Also you could cache the results of some expensive queries.
As long as /some code is not too much there is not really much you can do. These ajax calls area light-weight. Your problem is probably in the backend, where you send your requests to.
To debug/profile your problem, first check your ajax requests. Check them in a web-tool like Firebug and see that your request is expected. There you can also check which of your requests takes how long.
As you now see your bottleneck (e.g. requesting Contents.asmx/GetTitles is what takes so long), check your server that provides that page. As you did not provide code of that one, we can not help you there. But that’s where you have to look for next.

Categories