Ajax function error response (Network error occurs) waiting for 1 min.
If any function to quickly get for error response?
var $this=$(form);
$.ajax({
url: 'http://www.example.com/post.php',
dataType: 'text',
cache : false,
crossDomain : true,
type: 'post',
contentType: 'application/x-www-form-urlencoded',
data: $this.serialize(),
async:false,
success: function( data, textStatus, jQxhr ){
alert(textStatus);
},
error: function( jqXhr, textStatus, errorThrown ){
alert(errorThrown);
}
});
return false;
Related
.NET 6 (Core) MVC application. In view I have:
$("#mybtn").click(function () {
$.ajax({
type: "POST",
url: "/MyController/GetCustomer",
data: JSON.stringify({ 'id': 5 }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response, textStatus, xhr) {
alert(response);
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus);
}
});
});
and in controller:
[HttpPost]
public JsonResult GetCustomer(int? id) {
if (id == null) {
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { msg = "Error in the request." });
}
var customer = _db.Customers.Find(id);
return Json(customer);
}
It gets to the action method in controller, but the id is always null...
What am I doing wrong?
It gets to the action method in controller, but the id is always
null... What am I doing wrong?
To begin with, you don't need to use JSON.stringify({ 'id': 5 }) as { 'id': 5 } already in json format. In addition, contentType: "application/json; charset=utf-8", is uncessary as well, Instead, you can do as following:
Correct Format:
$("#mybtn").click(function () {
$.ajax({
url: "/MyController/GetCustomer",
type: "POST",
data: { 'id': 5 },
dataType: "json",
success: function (response, textStatus, xhr) {
alert(response);
},
error: function (xhr, textStatus, errorThrown) {
alert(textStatus);
}
});
});
Note: You are getting null in your controller because of using contentType: "application/json; charset=utf-8", and additional, Json parsing here JSON.stringify() these are only valid when you are sending parameter as object fashion. Just get rid of those two part, you are done.
Output:
I would like to mimic a post to a URL with the body type of plain text (such as "select * from table") using a Select2 box, but where do i define the body that I want to post?
$("#sales_ids").select2({
placeholder: "Select your Partners",
multiple: 'multiple',
ajax: {
url: 'http://www.google.com/,
dataType: 'json',
type: 'post',
multiple: 'multiple',
allowclear: 'true',
processResults: function p(data){
var mapped = $.map(data, function(obj) {
obj.text = obj.text || obj.name;
obj.id = obj.id || obj.salesId;
return obj;
})
return {
results: mapped
}
$.ajax({
url: 'URL',
type: 'POST',
dataType: 'json',
async: false,
// Data Goes Here
data: 'select * from table'
contentType: 'test/plain'
success: function (data, textStatus, xhr) {
// Success Code
},
error: function (xhr, textStatus, errorThrown) {
console.log('Error in Database');
}
});
my code is
$("#submit").click(function(){
var inId = $("#inId").val();
$.ajax({
url: 'http://boss.abc.com/x/Invoice/' + inId + '/5',
type: 'GET',
dataType: 'jsonp',
jsonpCallback: 'callback',
success: function (data) {
console.log(data);
}
});
});
It show url like http://boss.abc.com/x/Invoice/333/5?callback=callback&_=1440574382621
i'm trying to use ipinfodb for marking the user position on gmap.
my jquery code is
$.ajax({
type:"GET",
url:"http://api.ipinfodb.com/v3/ip-city/?key=<my api key>&ip=74.125.45.100",
crossDomain: true,
dataType: 'jsonp',
}
);
but i'm getting the following error on chrome
->Resource interpreted as Script but transferred with MIME type text/html: "http://api.ipinfodb.com/v3/ip-city/?key=141cb28778f3fe2d8e55fdd8e4511ad1777…25.45.100&callback=jQuery1820355858133174479_1352640806111&_=1352640806113".
->Uncaught SyntaxError: Unexpected number
what am i missing here?
var YOUR_KEY = theKey;//"123"
var theURL = "http://api.ipinfodb.com/v3/ip-city/?key=" + YOUR_KEY + "&format=json&callback=?";
$.ajax({
type: "POST",
url: theURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
method: 'GET',
success: function (data) {
alert('worked');
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert('failed');
}
});//$.ajax({
I couldnt find out what is the error.
<script type="text/javascript">
$(document).ready(function () {
$("#btnsumbit").click(function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
data: '{"username":"' + $("input#txtuser").val() + '","password":"' + $("input#txtpwd").val() + '"}',
url: 'http://localhost:53179/hdfcmobile/WebService.asmx/Login_Data',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success:
function (data, textStatus, XMLHttpRequest) {
var status = data.Status;
alert(data.d);
},
error:
function (XMLHttpRequest, textStatus, errorThrown) {
alert(textStatus);
}
});
});
});
</script>
I am getting 500 internal server error.How to call this webservice.I have passes the method with the url.Thanks for any help...
First thing, the way you are sending is wrong, send it like this
data: {
"username": $("input#txtuser").val(),
"password": $("input#txtpwd").val()
}
Next make sure, url: http://localhost:53179/hdfcmobile/WebService.asmx/Login_Data is returning JSON output.