AJAX Internal server error - 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.

Related

.NET 6 (Core) MVC application - problem with ajax call

.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:

How to invoke another function using a parameter fetched JSON result of API call from AJAX

I Have to get ParentId from this function using JSON result and then invoke another function given below after this function:
public fetchLibraryDatafromSharePointList(clientID:string) {
debugger;
const reactHandler = this;
jquery.ajax({
url: `${this.props.siteurl}/_api/web/lists/getbytitle('MSAs')/items?$filter=ClientID eq '${clientID}'&$orderby=Modified desc`,
type: "GET",
headers:{'Accept': 'application/json; odata=verbose;'},
success: function(resultData2) {
reactHandler.setState({
items: resultData2.d.results
});
},
error : function(jqXHR, textStatus, errorThrown) {
}
});
}
Below is the function which has to be invoked on the success of above API and the parameter is coming in the JSON result of the previous call made:-
SetState is done by reactHandler variable
public fetchDatafromSharePointList(ParentID) {
debugger;
const reactHandler = this;
jquery.ajax({
url:`${this.props.siteurl}/_api/web/lists/getbytitle('MSASummaries')/items?
$filter=Parent eq ${ParentID}$top=1&$orderby=Modified desc`,
type: "GET",
headers:{'Accept': 'application/json; odata=verbose;'},
success: function(resultData) {
/*resultData.d.results;*/
reactHandler.setState({
items: resultData.d.results
});
},
error : function(jqXHR, textStatus, errorThrown) {
}
});
}
You can invoke another function in the success response of fetchLibraryDatafromSharePointList(clientID:string) after updating state. i.e:
reactHandler.setState({
items: resultData2.d.results
}, ()=>{
fetchDatafromSharePointList(your_parent_id_goes_here);
});

Empty data result ajax with codeigniter

I'm trying to retrieve data from my controller function but this very simple example fails and sends me nothing back. The ajax function is successfully executed but the data is empty.
function company_select() {
var data;
var username = $('[name="username"]').val();
var url = base_url+'/admin/user/get_companies2';
alert(url);
$.ajax({
url: url,
type: 'POST',
dataType: 'text',
success: function(data){
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) { alert(textStatus + " " + errorThrown) }
});
}
codeigniter function
public function get_companies2(){
echo 'test';
}
You Should Use "data" instead of "dataType" Or Passed your require variables one by one as into curli braces of data (as data:{variable1:})
function company_select(){
var data;
var username = $('[name="username"]').val();
var url = base_url+'/admin/user/get_companies2';
alert(url);
$.ajax({
url: url,
type: 'POST',
data: {username :username},
success: function(data){
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) { alert(textStatus + " " + errorThrown) }
});
}

Ajax error response too late?

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;

Simple jQuery / Ajax error

I have this code:
var custID = 1;
$.ajax({
url: 'php/viewCustomer.php',
type: 'GET',
data: '{custID: ' + custID + '}',
dataType: 'json',
cache: false,
beforeSend: function () {
$('#display').append('<div id="loader"> Lodaing ... </div>');
},
complete: function () {
$('#loader').remove();
},
success: function (data) {
//do something
},
error: function () {
alert('could not process');
}
});
there is an error and alerts the error message could not process, so I tried to debug it like this:
var custID = 1;
$.ajax({
url: 'php/viewCustomer.php',
type: 'GET',
data: '{custID: ' + custID + '}',
dataType: 'json',
cache: false,
beforeSend: function () {
$('#display').append('<div id="loader"> Lodaing ... </div>');
},
complete: function () {
$('#loader').remove();
},
success: function (data) {
//do something
},
error: function (jqXHR) {
alert('Error: ' + jqXHR.status + jqXHR.statusText);
}
});
which outputs:
200 OK
so if it is ok, why on earth is it executing the error: function. Confused, please help.
Your data string is incorrectly formatted, if you are intending it to be a JSON object. There was a previous question about this: Jquery passing data to ajax function
Instead, try:
data: JSON.stringify({custID: custID}),
The format is (key):(variable). My previous answer have placed quotes around the variable, which is not necessary.

Resources