I have a ASP.Net project(not website). I have added the JQuery autocomplete widget to my master page and use the widget in MyPage.aspx.
In the .master I use something like this:
$.ajax({
url: "/MyService.asmx/MySearchMethod",
data: "{ 'param1': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
The MyService.asmx points to:
CodeBehind="~/App_Code/MyServiceMethods.cs" Class="MySearchMethod"
Everything works fine while in VS2010 development.
The problem is when I deploy the app to a test web server, I get the below error:
System.InvalidOperationException: No web service found at: /MyService.asmx
How can I reference my web service in the ajax url: so that the address can be resolved when the app is deployed?
try to use it as follows:
$.ajax({
url: "MyService.asmx/MySearchMethod",
data: "{ 'param1': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
or:
$.ajax({
url: "~/MyService.asmx/MySearchMethod",
data: "{ 'param1': '" + request.term + "' }",
dataType: "json",
type: "POST",
contentType: "application/json; charset=utf-8",
dataFilter: function (data) { return data; },
Related
I have these method in c# which requires 3 parameters
public void Delete_AgentTools(int ID,int UAM,int mode)
{
some code etc.
}
and I use javascript ajax to call this method and pass the parameter
function Delete_AgentTools(toolAccess, toolId, UAM) {
$.ajax({
type: "POST",
url: "IROA_StoredProcedures.asmx/Delete_AgentTools",
data: "{'toolAccess':'" + toolAccess + "', 'toolId':'" + toolId + "', 'UAM':'" + UAM + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success:function()
{
alert("Tool has been successfully delete");
},
error: function (XMLHttpRequest)
{
alert("error in Delete_AgentTools()");
console.log(XMLHttpRequest);
}
});
}
you see for me I want to simpilfy on how I pass the parameter in javascript. Is it possible to pass it as an object to the c# or simplify the passing of parameters in javascript
You can convert a js object as a JSON using JSON.stringify
var data = {};
data.toolAccess = value1;
data.toolId = value2;
data.UAM = value3;
$.ajax({
type: "POST",
url: "IROA_StoredProcedures.asmx/Delete_AgentTools",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success:function()
{
alert("Tool has been successfully delete");
},
error: function (XMLHttpRequest)
{
alert("error in Delete_AgentTools()");
console.log(XMLHttpRequest);
}
});
function Delete_AgentTools(toolAccess, toolId, UAM) {
var data = {};
data.mode = toolAccess;
data.ID = toolId;
data.UAM = UAM;
$.ajax({
type: "POST",
url: "IROA_StoredProcedures.asmx/Delete_AgentTools",
data: JSON.stringify(data),
contentType: "application/json; charset=utf-8",
dataType: "json",
success:function()
{
alert("Tool has been successfully delete");
},
error: function (XMLHttpRequest)
{
alert("error in Delete_AgentTools()");
console.log(XMLHttpRequest);
}
});
No need to change in your C# code.
i need to read string from my customer server.
the string that i need to call is:
http://xxx.xxx.xxx.xxx:8082/My_ws?applic=MyProgram&Param1=493¶m2=55329
The result I get is a string.
If I run it in the browser I get an answer string - OK
i need to get it in my HTML & javascript program
i try this:
function Look() {
$.ajax({
ServiceCallID: 1,
url: 'http://xxx.xxx.xxx.xxx:8082/My_ws?applic=MyProgram&Param1=493¶m2=55329',
type: 'POST',
data: '{"Param1": "' + 2222 + '"}',
data: '{"Param2": "' + 3333 + '"}',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success:
function (data, textStatus, XMLHttpRequest) {
ALL = (data.d).toString();
},
error:
function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
}
Try this, you need to use stringify if the data is Json otherwise remove JSON.stringify, you cannot have 2 data parameters either, your using utf-8 presume that is a MS web srvc, if your sending data up it's post (usually).
$.ajax({
type: 'Post',
contentType: "application/json; charset=utf-8",
url: "//localhost:38093/api/Acc/", //method Name
data: JSON.stringify({ someVar: 'someValue', someOtherVar: 'someOtherValue'}),
dataType: 'json',
success: someFunction(data), // pass data to function
error: function (msg) {
alert(msg.responsetext);
}
});
I create a new WebForms Application in VS2013. I didn't change anything and created a simple page. I want to load this table on client side when user clicks button
<table id="tblHelpRow">
<thead>
<tr class="title">
<th>F2
</th>
<th>F3
</th>
</tr>
</thead>
<tbody id="helpRowBody">
<%=MyRow %>
</tbody>
</table>
<asp:LinkButton ID="lnkEdit" runat="server" onclick="fnEdit();" />
This is my script code:
function fnEdit() {
BindGridView();
};
function BindGridView() {
rowid = {rowID:2};
$.ajax({
type: "POST",
url: "Default.aspx/GetRow",
contentType: "application/json; charset=utf-8",
data: param,
dataType: "json",
success: function (data) {
alert(data);
}
});
}
I have a WebMethod in my code-behind which stores result in the public property. DataSource I store in session, but rowID I need to pass from jquery.
[WebMethod]
public static string GetRow(int rowID)
{
DataTable dt = (DataTable)HttpContext.Current.Session["SourceData"];
MyRow = "<tr>" +
"<td>" + dt.Rows[rowID]["F2"].ToString() + "</td>" +
"<td>" + dt.Rows[rowID]["F3"].ToString() + "</td>" +
"</tr>";
return "done";
}
But I don't get any result. When I have put the breakpont in success I got "Authentication failed" error and this webmethod wasn't executed. What's the problem? I didn't change ant Authentication settings.
Try removing the ScriptMethod attribute. You are specifying a POST action type but I believe the ScriptMethod forces the request to be a GET by default. Also, I believe your param needs to be a JSON string instead of just an integer:
var param = {rowID:2};
$.ajax({
type: "POST",
url: "Default.aspx/GetRow",
contentType: "application/json; charset=utf-8",
data: JSON.stringify(param),
dataType: "json",
success: function (data) {
alert(data);
}
});
In my VS2013 web forms project the culprit turned out to be:
var settings = new FriendlyUrlSettings {AutoRedirectMode = RedirectMode.Permanent};
Using default settings for FriendlyUruls solved the problem--do not use RedirectMode.Permanent.
The ajax call like this, where the data params are complex.
$.ajax({
type: "POST",
contentType: "application/json",
url: applicationBaseUrl + "mypage.aspx/Calc",
data: params // data is json
}).success(function (data, status, xhr) {
//...
}).error(function (xhr, status, error) {
//...
});
The WebMethod like this
[WebMethod]
public static string Calc(IEnumerable<AiApp> aiApps, Guid guid)
{ //...
Use
$.ajax({
type: "POST",
url: "Default.aspx/GetRow",
contentType: "application/json; charset=utf-8",
data: {rowID:2},
dataType: "json",
success: function (data) {
alert(data);
}
});
I've looked at the previously-posted jQuery/MVC questions and haven't found a workable answer.
I have the following JavaScript code:
$.ajax({
type: "POST",
url: '#Url.Action("Search","Controller")',
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
}
});
When calling the Url the post looks like:
NetworkError: 500 Internal Server Error -
Why does it return it like this (the logic behind it) and what's a solution?
P.S.: Additional Information: %22 is the URL Encoding Reference for <<">> character
In order for this to work that JavaScript must be placed within a Razor view so that the line
#Url.Action("Action","Controller")
is parsed by Razor and the real value replaced.
If you don't want to move your JavaScript into your View you could look at creating a settings object in the view and then referencing that from your JavaScript file.
e.g.
var MyAppUrlSettings = {
MyUsefulUrl : '#Url.Action("Action","Controller")'
}
and in your .js file:
$.ajax({
type: "POST",
url: MyAppUrlSettings.MyUsefulUrl,
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
}
});
or alternatively look at levering the framework's built in Ajax methods within the HtmlHelpers which allow you to achieve the same without "polluting" your Views with JS code.
you have an type error in example of code. You forget curlybracket after success
$.ajax({
type: "POST",
url: '#Url.Action("Search","Controller")',
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
}
})
;
A good way to do it without getting the view involved may be:
$.ajax({
type: "POST",
url: '/Controller/Search',
data: { queryString: searchVal },
success: function (data) {
alert("here" + data.d.toString());
}
});
This will try to POST to the URL:
"http://domain/Controller/Search (which is the correct URL for the action you want to use)"
Starting from Rob's answer, I am currently using the following syntax.Since the question has received a lot of attention,I decided to share it with you :
var requrl = '#Url.Action("Action", "Controller", null, Request.Url.Scheme, null)';
$.ajax({
type: "POST",
url: requrl,
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
}
});
starting from mihai-labo's answer, why not skip declaring the requrl variable altogether and put the url generating code directly in front of "url:", like:
$.ajax({
type: "POST",
url: '#Url.Action("Action", "Controller", null, Request.Url.Scheme, null)',
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
}
});
Simple way to access the Url Try this Code
$.ajax({
type: "POST",
url: '/Controller/Search',
data: "{queryString:'" + searchVal + "'}",
contentType: "application/json; charset=utf-8",
dataType: "html",
success: function (data) {
alert("here" + data.d.toString());
});
I am trying to add header to ajax request.
I use setRequestHeader but it do nothing. I can look in firefox that request headers doesnot contains Authentication! What i am doing wrong?
And how to add header?
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://test/test.svc/news",
dataType: "json",
beforeSend : function(xhr) {
xhr.setRequestHeader("Authentication", "Basic " + Base64.encode(username + ":" + password))
},
success: function(data) {
alert('Success');
},
error: function() {
alert("Loading data error...");
}
});
}
Try username and password instead
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "http://test/test.svc/news",
dataType: "json",
username: 'username', /* not user */
password: 'pass',
success: function(data) {
alert('Success');
},
error: function() {
alert("Loading data error...");
}
});
}
username -
A username to be used in response to an HTTP access authentication request