I am writing a basic GET call using jquery AJAX in C# using MVC 4.
My code looks like this:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/GetData",
content: "application/json; charset=utf-8",
dataType: "json",
cache: false,
success: function (success) {
var jsonData = success;
}
});
});
</script>
But I am getting following error:
Can someone please help me here. I have already spent a day with no luck yet.
Related
I have a .net project in the framework 4.5.1
When I use ajax like below, it works and the debugger hits the page_load event:
$.ajax({
"async": true,
"crossDomain": true,
type: "POST",
url: "Advanced.aspx",
contentType: "application/json; charset=utf-8",
success: function (ms) {
alert('success');
}
})
In my advanced.aspx.cs, I have the method below:
[System.Web.Services.WebMethod, ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
public static string SaveChange(string myval)
{
//dummy code for test purposes.
string u = myval + "X";
return u;
}
I want to hit this method via ajax. To do this, I am updating my ajax call like below:
$.ajax({
"async": true,
"crossDomain": true,
type: "POST",
url: "Advanced.aspx/SaveChange",
data: {username:"test"},
contentType: "application/json; charset=utf-8",
success: function (ms) {
alert('success');
}
})
However, it gives me 401:unauthorized error when i do it in that way, shown below:
How can I fix this? Tried lots of things for the last few hours, no luck.. Any help would be appreciated.
EDIT: I've used async and crossDomain hoping to fix this issue, didn't help. Normally they don't exist in my ajax call, nothing changes.
Found the answer here: ASP.NET Calling WebMethod with jQuery AJAX "401 (Unauthorized)"
I have used
settings.AutoRedirectMode = RedirectMode.Off; instead of RedirectMode.Permanent in ~/App_Start/RouteConfig.cs, which fixed the issue.
Try updating your AJAX JS to:
$.ajax({
type: "POST",
url: "Advanced.aspx/SaveChange",
data: {myval:"test"},
success: function (ms) {
alert('success');
}
})
I am trying to post json to an api. As well as get get html form data and convert it to json. But i cant make a simple ajax post and then return an alert box.
What is wrong with this?`
<script type="text/javascript">
$( document ).ready(function() {
$('#submit').click(function() {
$.ajax({
type: "POST",
url: "test url",
data: "hahaaha",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(data){alert(data);},
failure: function(errMsg) {
alert(errMsg);
}
});
});
});
</script>`
There's no failure function on the jQuery AJAX documentation, are you perhaps referring to error?
Trying the basic stuff,
request with data and response with data and print it with jQuery and Rails
This is the front code.
$("#internal_btn").click(function() {
//window.alert("clicked internal btn!");
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "/room/test",
//data: "{'data1':'" + value1+ "', 'data2':'" + value2+ "', 'data3':'" + value3+ "'}",
data: {name:"ravi",age:"31"},
dataType: "json",
success: function (result) {
//do somthing here
window.alert("success!!");
},
error: function (){
window.alert("something wrong!");
}
});
});
in here, if the user clicks internal_btn this event happens and goes to the servide
room/test action.
ok this is fine. But I'm not sure how to send the data.
If i run this, i have an error like this.
MultiJson::LoadError
795: unexpected token at 'name=ravi&age=31'
Can i know what the problem is?
Also, is there are good example with this request and response with json format?
I googled a lot, but not satisfied with the result :(
Try to use stringify your data or use GET method like,
data : JSON.stringify({name:"ravi",age:"31"}),
Full Code,
$.ajax({
type: "POST",// GET in place of POST
contentType: "application/json; charset=utf-8",
url: "/room/test",
data : JSON.stringify({name:"ravi",age:"31"}),
dataType: "json",
success: function (result) {
//do somthing here
window.alert("success!!");
},
error: function (){
window.alert("something wrong!");
}
});
I am trying to call a web-service which has a Json output from JavaScript. But I am unable to get the value. I tried with different methods but have been unsuccessful. Please help !!
Here is the code I tried
<script src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function callajax(){
var html =$.ajax({
cache: false,
type: "GET",
async: false,`enter code here`
data: {},
url: "http://domain/abc.php?param=abcd',
contentType: "application/json; charset=utf-8",
dataType: "json",
crossDomain: true,
Success: SucceedFunc,`enter code here`
error: function (request, errorText, errorCode)
{
alert(errorText+"--"+errorCode);
}
});
}
function SucceedFunc(data, status)
{
alert("Enter into Success");
alert(data);
}
Desired output is in {"name":Alex,"Success":true} format.
I need to pick value for "name".
Help would be appreciated.
Is it because your Success: is capitalized? Should be success:.
EDIT:
Also, is there a reason you're using such an old version of jQuery? They're at 1.9.x now (for most uses). I just looked it up and the crossDomain option was added in 1.5.
I have a JSON response coming from REST web service and want to bind that data to html using jQuery. Looks like its not even hitting web service url which I have provided in my jquery.
Url is working fine which gives me JSON data in browser but jQuery I am using unable to get any content from this. I am pasting my code here, plz let me know if some one can help.
While debugging script its directly going on error section in ajax call.
<script type="text/javascript">
$(document).ready(function () {
GetData();
});
function GetData() {
// alert(textblock.value);
$.ajax({
type: "GET",
url: "http://localhost:8092/api/Employees",
data: "{'employeeId'= '" + 1234 + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (msg) {
var results = $.parseJSON(msg.d);
alert(msg);
alert(results);
},
error: function (result) {
alert('here');
var tt = result.text;
alert(tt);
}
});
}
</script>
finally i am able to get data now.
I added below properties in $.ajax:
processData: false,
async: false,