Can't able to get the data using ajax in the MVC - javascript

I am using AJAX call in JS and calling a controller and action method with the url
I ran the code in my local it is working fine but when it was deployed in production the AJAX call didn't get the data and it is throwing a message in console
Could not load content for https://az416426.vo.msecnd.net/scripts/JavaScript/JavaScriptSDK/ajax/ajax.ts (HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE)
The data on local is limited but it has more data on production
I tried it by calling through AJAX and from the action method I am returning the data in the JSON format and the data i recieved is parsed and then it is sent to a hidden field it was running fine on my local all the data is there but when it was deployed on production after the page load the ajax will be called but I can't able to see the data and it shows the error which I mentioned above.

I will give you two recommendations that work better for me when dealing with passing JSON data from a controller.
First try this on your get data method:
[HttpGet]
public ContentResult Getsubscriptiondata()
{
var subdata = db.ExistingSubscriptionList.Where(substatus => substatus.OSCP_SubStatusId == "1").ToList();
return Content(JsonConvert.SerializeObject(subdata));
}
Second in your javascript change the line
document.getElementById('hdnExistingSubs').value = JSON.stringify(data)
to
document.getElementById('hdnExistingSubs').value = JSON.parse(data)
This gives me more consistent results when receiving data from the controller, hope this helps.
Clarification: I prefer this methods because passing a Json result sometimes haves quirks in the way the data is rendered on the javascript in the browser.

Related

Passing JS variables to a PHP file (using ajax) in order to update SQL table [duplicate]

I have a problem sending data with an AJAX query to a php file, basically I use post request to send some data just for testing it out, the request is successful when I checked with developer tools in Chrome, as I can see the data that was sent , but the variable $_POST[] in php is always null, and don't understand why, because my data was sent to the php file.
Tried all kind of possibilities found on here, non of them will let me to go further, all of them will leave my $_POST[] empty. I modified the values inside the data attribute I added or removed content-type but nothing worked.
Here you have my jQuery code.
$.ajax({
method: "POST",
url: "2.php",
data: { name: 'JohnDoe', age: '19' }
}).done(function( msg ) {
alert(msg);
});
This is my PHP code.
<?php
$user=$_POST['name'];
var_dump($user);
?>
Errors
$user=$_POST['name'];
//ERROR - Notice: Undefined index: name in G:\xampp\htdocs\weather\2.php on line 6
//Expected result is 'JohnDoe'.
var_dump($user);
//this is Null
//Expected result is to contain some data
This are my two results I get in php.
The html and php files are in the same folder.
You are making two HTTP requests.
The first one using JavaScript, where you make a POST request and alert the response.
You can see the data from the response there.
You make the second request by typing the address into the address bar, where you make a GET request and have the response rendered as a webpage.
$_POST contains the POSTed data from the current request, not the data from any previous request.
The data you POSTed when you made the first request is not available when the PHP program runs again using the second request as input.
If you want to access that data then you need to explicitly do something to make it persist. This could be linked to the browser (so different users would not see each others data) — such as in a session or a cookie — or it could be independent of the browser — such as in a database — so every visitor to the site could see the data.

Fetch/XHR data generated after execution of function using Python and Selenium

I have a website opened using Python and chromedriver. This website loads variables and you can make use of its javacript files to execute its functions (well this website and all others as it is usual behaviour)
If I do the following
driver=webdriver.Chrome()
driver.get(www.myurl.com)
script="const xhrfunction= new websitevariable.some_class; var a=entry data; xhrfunction.load(a); return xhrfunction"
driver.execute_script(script)
I see in the devtools in the network tab that a new fetch/xhr line has been generated and that the response is a long string with data. However, I can't get this string of data returned from driver.execute_script(script)
When it is possible to perform the fetch directly, I do the following and it generates the exact same line in the network tab and also returns me the data from the response
script='var url= myurlhere; resp= await fetch(url); return {'status':resp.status, 'data': await resp.text()}
driver.execute_script(script)
But that is not an option so I need to get this string of data generated by the execution of my function in the first code snippet.
How would I accomplish this using execute_script?

JSON Reader issue: cannot access jsonData

I have a strange thing going on here: I am using a JSON reader within a store to fetch search results. After loading the store I receive data or error states (built together as a JSON, too). So in both ways I get a successfully response, so I have to check the JSON for myself to trap "error conditions". But I cannot access the jsonData property that shhould be a JSON object within the reader. Chrome tells me that:
I can access the applyDefaults though (it returns true in that case) but not the jsonData.
My code looks like this:
var result = searchStore.getProxy().getReader();
console.log(result.jsonData);
The output is "undefined". As you can see in the picture the jsonData object holds my JSON (with the isError property I wanted to access).
What I am doing wrong?
You need to think more async and think of the timing it takes for a request to return and when you are trying to get the jsonData. Instead of using console.log, set a breakpoint or use the debugger; statement so you can freeze the browser and walk through the code. You can then inspect variables and such to see what the object looks like.
Try to access your JSON data in a success callback, to make sure the data is gathered from the server.

Data is returned from resource, but nothing displays

I have an AngularJS Frontend/Rails backend web app.
I posted my frontend code on JSFIDDLE. My rails backend on the index page returns some json:
[{"id":1,"name":null,"user_id":null,"created_at":"2013-11-27T22:09:31Z","updated_at":"2013-11-27T22:09:31Z"},{"id":2,"name":null,"user_id":null,"created_at":"2013-11-27T22:22:55Z","updated_at":"2013-11-27T22:22:55Z"}]
On the network tab on Chrome Developer, it shows that the response is returning the correct JSON. There isn't any errors (at least that it shows), but nothing displays like it should when I use {{row.id}} or {{row.created_at}}, which are filled with data in the json.
Thanks for all help!
Most probably you are missing $scope.$apply() statement in your callback as the callback is from outside angularjs context. Try this
rowsService.fetchPopular(function(data){
$scope.$apply(function() {
$scope.rows = data;
});
});

Working with JSON via GET requests in Javascript

I've built a php API that provides data in json output, I need to get the values via a get request to then plot as a graph on the page.
The front end web component in hosted on the same server as in the api in this basic structure:
index.php
graph.php
/api/
/api/src
/api/src/api.php
My current code in graph.php is as follows:
<script>
var myJson;
$.getJson('api/src/api.php/poll/results/current/13/', function(jd){
myJson = jd.AnswerCount.1;
});
document.getElementById('jsonhere').innerHTML = myJson; //just to test
</script>
The endpoint outputs data like the following:
{"AnswerCount":{"1":5,"3":1,"2":2,"4":1,"5":5,"6":3,"7":2}}
Which I need loaded into a key-value pair array,
1:5
3:1
2:2
4:1
...
to then be put into the graphing library.
How do I fix my code/write new code to do this? I'm pretty stuck here.
EDIT:
On a hunch I logged all the get requests via wireshark, and no request is ever sent to the url in question. Even with an empty function { } ? http://grab.kfouwels.com/pmgW
You can't use a number as an identifier, to access the 1 property you have to say [1] not .1
You have to use the variable containing your data, not x which hasn't been mentioned until you try to assign it somewhere
The A in Ajax stands for Asynchronous. You have to work with your data inside your callback since the function you pass to getJson won't be called until the HTTP response arrived but the line starting document.get will run as soon as the HTTP request has been sent.

Categories

Resources