I'm migrating a site to django, and part of that is some ajax calls to php scripts. All I keep getting back from my ajax calls are the contents of the php file, not the results of the script being executed.
Not sure if this is because I'm only using the django dev server (project not in a folder processed by apache) or if I need url.py and views.py entries for the ajax call...
My ajax call from my js file:
function getLab(labId) {
let data = new Object();
data['ID'] = labId;
$.ajax({
url: "/static/php/fetchLab.php",
type: "get",
data: data,
success: getLabFinish
});
}
And the response is the contents of the php file:
<?php
require('/static/php/log.php');
require('/static/php/config.php');
$log = new Log();
...
For this to work, something must take this PHP script(s) and pass them to an PHP interpreter. Otherwise you receive the script as text, as the server threats them like that. You might have encountered the same effect when you serve PHP files with a webserver that is not configured with PHP properly.
Your guess regarding apache isn't that wrong. You might configure a local apache with the PHP module enabled to receive their desired output.
This approach might be sufficient while migrating, but it is definitely not recommended as a result of your migration.
As a workaround you might also replace the PHP script with dummy output and provide them in the static folder. This way you can already prepare other parts of your application and migrate them later.
As already mentioned you want to end up with corresponding views in your django project.
I agree with what dahrens and Jerin Peter George have said. It definitely sounds like you do not have PHP installed on the server, which wouldn't be needed in a python/django environment. Therefore the ajax request is getting back text from the php file.
I will add some context for you to get an idea of what an ajax request and response would look like in django 2.x
in your javascript (frontend)
$.ajax({
url: '/save/json/',
method: 'POST',
data: {"data":"content"}
})
in your views.py
def save_json(request):
if request.is_ajax():
if request.method == 'POST':
incoming_json_data = json.loads(request.body)
# DO STUFF WITH THE JSON DATA
HttpResponse("OK")
in your urls.py
urlpatterns = [
path('save/json/', views.save_json),
]
Related
I’m working on a project that uses SharePoint 2010. I need to write a POST variable to a file using an ajax call.
If I were using PHP I would use the fwrite() function to write the POST to a file.
Here is how I envision my solution working. When you go to notarealwebsite.com and submit the form, I envision using an ajax call to write the file. The ajax on the index.php would look like:
$.ajax({
type: 'POST',
url: 'save-text.php',
data: {json: JSON.stringify(strJson)}
});
In PHP I would pass the POST variable into the save-text.php file and its code would look like this:
<? php
$file = fopen("file.txt","w");
fwrite($file, $_POST['json']);
fclose($file);
?>
Does SharePoint have an equivalent function I can use to save the POST to a file?
You don't give us too much here...
You can develop something in JavaScript using a third library like SharepointPlus with the createFile function
You'll do:
$SP().createFile({
content: JSON.stringify(strJson),
destination: "http://mysite/Shared Documents/file.txt",
url:"http://mysite/",
after:function() {
alert("File saved");
}
});
The createFile of SharepointPlus uses the CopyIntoItems web service of Sharepoint.
In SharePoint environment, you would most probably write these strings to list instead of file system. This is especially true if its some kind of configuration value because you want it accessible from all the frond end machines. A file on a filesystem will be created on just a single node where the code runs ultimately after the load balancer.
See:
How to: Create, Update, and Delete List Items
http://msdn.microsoft.com/en-us/library/office/ee539976(v=office.14).aspx
I know there are several screen scraping threads on here but none of the answers quite satisfied me.
I am trying to scrape the HTML from an external web page using javascript. I am using $.ajax and everything should work fine. Here is my code:
$.ajax({
url: "my.url/path",
dataType: 'text',
success: function(data) {
var myVar = $.get(url);
alert(myVar);
}
});
The only problem is that it is looking for the specified url within my web server. How do I use a proxy to get to an external web page?
Due to Cross Site Scripting restrictions, you're going to have to pass the desired URL to a page on your server that will query the URL in question from serverside, and then return the results to you. Take a look at the thread below and the incorporate that into your application and have it return the source when that page is hit by your AJAX function.
How to get the HTML source of a webpage in Ruby
Using a GET request is going to the be easiest way to transfer the URL of the page you want to fetch your server so you'll be able to call something like:
$.ajax("fetchPage.rb" + encodeURI(http://www.google.com))
Because you can't access the side in question directly from the server, you're going to have to pipe the serverside script through a proxy for the request to work, which really kind of depends on your setup. Taking a look at the Proxy class in Ruby:
http://ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/Net/HTTP.html#method-c-Proxy
My application is located on the server under a separate virtual directory. To access my ASP.NET MVC application users have to go to:
http://server-dev/superApp
I have a problem with Ajax/Json server requests ignoring the "superApp" directory part. Whenever an Ajax request is made Fiddler shows 404 because instead of http://server-dev/superApp/User/GetUsersJson for example, http://server-dev/User/GetUsersJson is called (note the missing superApp name).
Example of an Ajax request:
function GetUsers(id) {
$.ajax({
url: "/User/GetUsersJson/",
data:{ id: id},
datatype: 'json',
type:'post',
success: function (result) {
////Do stuff with returned result
}
});
}
Registered route:
r.Match("User/GetUsersJson", "User", "GetUsersJson");
Where should I look and what can I change to make sure that my application virtual folder is ALWAYS included in all URL requests ?
p.s. Please note that all Javascript/Ajax logic is kept in separate .js files so no RAZOR syntax is available.
Did you try using the HTML helper method ?
url: "#Url.ACtion("GetUsersJson","User)"
EDIT : As per the comment
You may get the Path name using the HTML Helper method and Keep that in a Global variable and access that in the external javascript file
In the view
<script type="text/javascript>
var globalGetJSONPath='#Url.ACtion("GetUsersJson","User)';
</script>
And now you can use it in the external file like this
$.ajax({
url: globalGetJSONPath,
data:{ id: id},
//remaining items....
});
I solved this stuff by passing variable to js that contains hostname+vdir. Because of heavy js url generation.
In other cases Shyju's answer is best way to solve this.
No way to do it without some server-side code generation. Easiest thing would be defining global variable (sorry) holding you application root and initializing it somewhere in master page.
Javascript generation of route urls always was one of the messiest parts of asp.net mvc.
Here is my situation:
Im creating a widget that site admins can embed in their site and the data are stored in my server. So the script basically has to make an ajax request to a php file in my server to update the database. Right? Right :)
The ajax request works excellent when i run it in my local server but it does not work when the php file is on my ONLINE server.
This is the code im using:
var url = "http://www.mydomain.net/ajax_php.php";
var params = "com=ins&id=1&mail=mymail#site.net";
http.async = true;
http.open("POST", url, true);
http.onreadystatechange = function() {
if(http.readyState == 4 && http.status == 200) {
//do my things here
alert( http.responseText );
}
}
http.send(params);
In firebug it shows: http://www.mydomain.net/ajax_php.php 200 OK X 600ms.
When i check the ajax responnseText I always get a Status:0
Now my question is: "Can i do cross-domain ajax requests by default? Might this be a cross-domain ajax problem? Since it works when the requested file resides in my local server but DOESN'T work when the requested file is in another server, im thinking ajax requests to another remote server might be denied? Can you help me clear on this?
Thanks..
Cross-domain requests are not directly allowed. However, there is a commonly-used technique called JSONP that will allow you to avoid this restriction through the use of script tags. Basically, you create a callback function with a known name:
function receiveData(data) {
// ...
}
And then your server wraps JSON data in a function call, like this:
receiveData({"the": "data"});
And you "call" the cross-domain server by adding a script tag to your page. jQuery elegantly wraps all of this up in its ajax function.
Another technique that I've had to use at times is cross-document communication through iframes. You can have one window talk to another, even cross-domain, in a restricted manner through postMessage. Note that only recent browsers have this functionality, so that option is not viable in all cases without resorting to hackery.
You're going to need to have your response sent back to your client via a JSONP call.
What you'll need to do is to have your request for data wrapped in a script tag. Your server will respond with your data wrapped in a function call. By downloading the script as an external resource, your browser will execute the script (just like adding a reference to an external JS file like jQuery) and pass the data to a known JS method. Your JS method will then take the data and do whatever you need to do with it.
Lots of steps involved. Using a library like jQuery provides a lot of support for this.
Hope this helps.
I am trying to make a RESTful webservice call using Extjs. Below is the code i am using:
Ext.Ajax.request({ url: incomingURL ,
method: 'POST',
params: {param1:p1, param2:p2},
success: function(responseObject){
var obj = Ext.decode(responseObject.responseText);
alert(obj);
},
failure: function(responseObject){
var obj = Ext.decode(responseObject.responseText);
alert(obj);
}
});
but it does not work, the request is sent using OPTIONS method instead of POST.
I also tried to do the same thing using below code but result is the same:
var conn = new Ext.data.Connection();
conn.request({
url: incomingURL,
method: 'POST',
params: {param1:p1, param2:p2},
success: function(responseObject)
{
Ext.Msg.alert('Status', 'success');
},
failure: function(responseObject)
{
Ext.Msg.alert('Status', 'Failure');
}
});
But when i tried to do the same thing using basic ajax call ( using the browser objects directly i.e. XMLHttpRequest() or ActiveXObject("Microsoft.XMLHTTP")) it works fine and i get the response as expected.
Can anyone please help me, as i am not able to understand what i am doing wrong with extjs ajax call?
You can't make a standard AJAX call between domains. The URL for Ext.Ajax.request should be a relative one (relative to the script's origin).
If you want to do cross-domain calls, use a ScriptTagProxy or such.
The problem is exactly because of the reason ob1 and Chuck Hinson described.
I have an RESTful service, wich is running on Tomcat.
And i made a static client(no deployed to Tomcat) using ExtJs with Json reader.
I just made an html page with ExtJs integrated consuming REST service like url: http://localhost:8080/service/invoices/
And all the time ExtJs was making OPTIONS request, not GET or POST even if i was setting them as being used methods. The problem is this security feature, because Client is not the part of same application and i am doing AJAX call between domains.
As soon as i put my client to my Web application and deployed to Tomcat and started using relative calls it started working.
if you don't want cross-domain request, please remove the website prefix 'http://website' from propery url of ajax proxy.