ReferenceError: onclick function is not defined - javascript

I've gotten a "ReferenceError: function is not defined on the saveRoute function". Everything seems right to me but i could not figure out the problem.
Here is the related codes.
<script type="text/javascript">
var array = routeobj;
function saveRoute(){
var json = JSON.stringify(array)
$.ajax({
url : "http://192.168.1.9:11123/runornot/drawtrack",
type: "POST",
dataType:'json'
data : {json:json}
{"routeJson": json },
console.log("hellohelloworldolr");
success: function(data, textStatus, jqXHR)
{ //data - response from server
console.log("checkin success"); },
error: function (jqXHR, textStatus, errorThrown)
{
}});}
</script>
and in the html
Save

<script type="text/javascript">
var array = routeobj;
function saveRoute(){
var json = JSON.stringify(array) // <--- best practice would require a semicolon here
$.ajax({
url : "http://192.168.1.9:11123/runornot/drawtrack",
type: "POST",
dataType:'json' // <--- missing a comma
// the structure doesn't make any sense starting here...
data : {json:json} // <--- should be a comma here?
// missing property name?
{"routeJson": json },
// is this supposed to be the body of a function?
console.log("hellohelloworldolr");
// things start making sense again here...
success: function(data, textStatus, jqXHR)
{ //data - response from server
console.log("checkin success"); },
error: function (jqXHR, textStatus, errorThrown)
{
}});}
</script>

Related

Access post data from inside ajax error function

I have the below javascript function that takes POST data and sends post request to server using Ajax
function postData(post_data) {
console.log(post_data, "----------->");
var data = post_data;
var url = "/super/man/"
$.ajax(
{
type: "POST",
url: url,
data: post_data,
dataTpe: "json",
success: function (data) {
debugger;
},
error: function (jqXHR, textStatus, errorThrown) {
debugger;
// Can we access the post_data inside this error function ?
},
}
);
};
So what my actual point is, because of some reason the server is sending a 500 response and so the execution point is coming to error: function (jqXHR, textStatus, errorThrown, data), here I want to access post_data to display something to the user.... So can we access the post_data inside ajax error function above?
In case someone looks for a generic way to do this, here is how i did it: In case your handler functions are defined where their scope don't allow you to access some variables, you can add them to the ajax object itself in the function beforeSend. You can then retreive it in the ajax object by using this.
$.ajax({
url:'/dummyUrl',
beforeSend: function(jqXHR, plainObject){
plainObject.originalUrl = 'myValue';
},
success: function (response) {
$('#output').html(response.responseText);
},
error: function () {
$('#output').html('Bummer: there was an error!');
$('#myValue').html(this.originalUrl);
},
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="output">waiting result</div>
<div id="myValue"></div>
function postData(post_data) {
console.log(post_data, "----------->");
// var data = post_data; // why ?
var url = "/super/man/"
$.ajax(
{
type: "POST",
url: url,
data: post_data,
dataTpe: "json",
success: function (response) { // pay attention to args and vars naming as it makes the code easier to read
// use response
},
error: function (jqXHR, textStatus, errorThrown, data) {
// handle error
console.log(post_data); // and the magic happens
},
}
);
};
Above this issue you were having wrong key "dataType" i have modified it. Secondly, "post_data" is in your scope you can access it without any issue.
function postData(post_data) {
console.log(post_data, "----------->");
// var data = post_data; // why ?
var url = "/super/man/"
$.ajax(
{
type: "POST",
url: url,
data: post_data,
dataType: "json",
success: function (response) { // pay attention to args and vars naming as it makes the code easier to read
// use response
},
error: function ( jqXHR jqXHR, textStatus, errorThrown) {
// post data is in your scope you can easily access it
console.log(post_data); // and the magic happens
},
}
);
};

Getting no response from an ajax call

I have been trying to get this ajax call to work for several hours and have not been able to resolve any possible issues as everything appears to be coded properly according to jQuery Ajax Call. From what I can tell the code is not even being executed. However when I manually run the API call I get the following JSON result:
<UnitsOfMeasure.UnitsOfMeasureDataWithMessage xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Corporate.Web.API.DAL">
<Message/>
<Success>true</Success>
<UnitOfMeasureBaseUnitItem i:nil="true"/>
<UnitOfMeasureBaseUnitList i:nil="true"/>
<UnitOfMeasureRelatedUnitItem i:nil="true"/>
<UnitOfMeasureRelatedUnitList i:nil="true"/>
<UnitOfMeasureSetItem i:nil="true"/>
<UnitOfMeasureSetList i:nil="true"/>
<UnitOfMeasureTypeItem i:nil="true"/>
<UnitOfMeasureTypeList i:nil="true"/>
<UsedForID>d64d99f9-acfa-44cd-a199-4557b7b20912</UsedForID>
</UnitsOfMeasure.UnitsOfMeasureDataWithMessage>
My API Code is as follows:
[Route("api/admin/UnitsOfMeasure/UnitOfMeasureDefaultUsedForGetID")]
[HttpGet]
[ResponseType(typeof(UnitsOfMeasure.UnitsOfMeasureDataWithMessage))]
public IHttpActionResult UnitOfMeasureDefaultUsedForGetID(string UnitOfMeasureDefaultUsedForName)
{
UnitsOfMeasure _oUnitsOfMeasure = new UnitsOfMeasure();
return Ok(_oUnitsOfMeasure.UnitsOfMeasureSet_GetUsedForID(UnitOfMeasureDefaultUsedForName));
}
My DAL is supposed to return a Boolean flag and a Guid.
And my Ajax call is as follows:
$.ajax({
type: "GET",
url: g_WebServiceUnitOfMeasureDefaultUsedForGetIDURL,
data: {"UnitOfMeasureDefaultUsedForName": "Purchase"},
datatype: "json",
success: function(jsonResult){
DefaultUnitUsedForID = jsonResult;
},
failure: function (xhr, textStatus, errorThrown) {
console.log(xhr);
console.log("---------------------------------------------------------------------------");
console.log(textStatus);
console.log("---------------------------------------------------------------------------");
console.log(errorThrown);
console.log("---------------------------------------------------------------------------");
},
error: function (xhr, textStatus, errorThrown) {
console.log(xhr);
console.log("---------------------------------------------------------------------------");
console.log(textStatus);
console.log("---------------------------------------------------------------------------");
console.log(errorThrown);
console.log("---------------------------------------------------------------------------");
}});
As mentioned, from stepping through the code, it does not even enter the success or failure functions. What am I doing wrong?
May be it is foolish, But give it a shoot
$.get( g_WebServiceUnitOfMeasureDefaultUsedForGetIDURL,
{"UnitOfMeasureDefaultUsedForName": "Purchase"} )
.done(function( data ) {
DefaultUnitUsedForID = jsonResult;
alert( "Data Loaded: " + data );
});

Internal Server Error when response text from ajax POST call is big

I am using an ajax call like the following:
$(function () {
$.ajax({
type: 'POST',
url: 'frmHistoryReportFinal.aspx/GetDataTable',
data: json_data,
contentType: 'application/json;charset=utf-8',
dataType: 'json',
success: function (responseText) {
if (responseText.d == "")
return;
parsedData = JSON.parse(responseText.d);
alert(parsedData);
count = 0;
},
error: function (xhr, textStatus, errorThrown) {
//process the error
alert(xhr);
alert(textStatus);
alert(errorThrown);
}
});
});
When I have a small set of data returned from my C# codebehind. The ajax call functions well. But when I have a json string with larger data, say 200 records returned then the ajax call gives error("Internal Server Error").
Please help me resolve the issue as I usually need to handle large datasets.

Post multiple jquery with ajax to python

my question: I'm trying to post multiple data using ajax request. But I don't get anything back from python script. I get a error from console, it says:
data: { serializedData1, serializedData }
error: Uncaught SyntaxError: Unexpected token ,
But if I remove the , I will get more and more errors syntax errors.
If I edit the javascript like this: data: serializedData1 it wil work but it isn't what I want.
var valueID = $(this).parent().parent().find(".inputLiveID")// this is the id I want
var serializedData1 = valueID.serialize()
var $form = $(this); // I want this value too
var serializedData = $form.serialize();
request = $.ajax({
url: "/accounts/editnad/",
type: "post",
data: { serializedData1, serializedData }
});
request.done(function (response, textStatus, jqXHR){
console.log(response)
});
request.fail(function (jqXHR, textStatus, errorThrown){
console.error(
"The following error occured: "+
textStatus, errorThrown
);
});
python script:
def editnad(updatenad):
result = False
updid = updatenad.POST.get("newid")
newnr = updatenad.POST.get("nadcodenr")
newbesch = updatenad.POST.get("nadbeschikbaar")
return HttpResponse(updid,newnr)
You need to pass name/value pairs in your ajax call, for example
$.ajax({
url: 'myScript.py',
data: {form1: serializedData1, form2: serializedData},
success: function(response) {
//do something with response
}
});
Then in the python
updid = udatenad.POST.get('form1').updid
You'll also note I didn't assign the ajax call to a variable, I used a success callback instead. If you want to use a promise you can assign it, and then use resolution/broken callbacks. Guess I'm old school.
You define an array in JS with [], not {}.
data: [ serializedData1, serializedData ]

Post data from ajax request empty in PHP

Here is my javascript code :
var json = JSON.stringify(data);
$.ajax({
data: {data : json },
type: 'POST',
url : 'test.php',
success: function( data, textStatus, jqXHR) {
alert(data);
alert(textStatus);
alert(jqXHR);
},
error: function(jqXHR, textStatus, errorThrown) {
alert(jqXHR);
alert(textStatus);
alert(errorThrown);
}
});
Here is my test.php :
var_dump($_POST);
success function returns correctly the data
array(1) {
["data"]=>
string(51) "["image_0","image_1","image_2","image_3","image_4"]"
}
But in my test.php var_dump() return an empty array..
array(0) { }
What I'm doing wrong ?
Why cant you simplify like
$.ajax({
data: JSON.stringify(data)
...
});
It's ok I understand my mistake. Sorry but I'am newbie in javascript.
In my test.php:
if(isset($_POST["data"])) {
$_SESSION["test"] = $_POST["data"];
}
echo $_SESSION["test"];

Categories

Resources