I need to load a dynamic created javascript with PHP. Is there any good solution to load javascript creating by PHP after some ajax requests finished?
Thx in regards, i am searching for 2h in stackoverflow.
if i call .ajax function where html/js respone, jquery kills the js ;/
jQuery.ajax({
type: 'POST',
dataType: 'html',
url: $url,
success: function(result)
{
result // html
jQuery.ajax({
type: 'GET',
dataType: 'script',
url: $url,
success: function(result)
{
//the result is js and is availble in browser if you return
//a function you can execute this now
}
});
}
});
Hope someone find his answer here :) gl & hf & nice day
Related
I've been trying to call a VB.NET function from JavaScript for the website I've been working on, and found out about AJAX and started trying to use it.
(For reference, my target framework is 4.6.1.)
The following is a snippet of my VB.NET code, where PopulateDetailSection() is something which returns a string which is supposed to be the text from the div, but my breakpoint never hits this function.
System.Web.Services.WebMethod(EnableSession:=True, BufferResponse:=False)
Protected Shared Function PopulateDetail() As HtmlGenericControl
Return PopulateDetailSection()
End Function
and as for the AJAX call:
jQuery.ajax({
url: "frmActiveTrackingG.aspx/PopulateDetail",
type: "GET",
//contentType: "application/json: charset=utf-8",
dataType:"html",
success: function (data) {
alert(data);
}
});
I've tried alerting several things, but it keeps returning undefined unless I alert data which appears to return the aspx file starting with the header.
I usually don't ask questions here, but I'm truly stumped on this.
There some issues with your JavaScript. As described here: https://stackoverflow.com/a/5332290/428010 you need to post on the web page/method.
jQuery.ajax({
url: "frmActiveTrackingG.aspx/PopulateDetail",
type: "POST",
contentType: "application/json: charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
}
});
On the VB side the method need to be public not protected.
I need to send ajax post on ajax posted page.
index.php has
$.ajax({ type: "POST",datatype:"json", url: "/notification.php",
data: "kime=karaakce&tur=konuyayorum&postid=2020",
success: function(html){
}
});
notification.php has same function but posts to track.php
$.ajax({ type: "POST",datatype:"json", url: "/track.php",
data: "kime=karaakce&tur=konuyayorum&postid=2020",
success: function(html){
}
});
However, notification.php doesnt send the ajax post. How can I make it run ?
First thing you cannot run the jquery code even if you include it in your notification.php file. That is because jquery runs only in browser, not at backend. So unless you "physically" open the notification.php page in browser the jquery won't run.
So to address your issue you'll have to chain the success response from one php file to next.
eg:
Data from index.php ---> notification.php ---> index.php ---> track.php
(Although a very crude approach)
Here is the code that can achieve this.
index.php file
$.ajax({
type: "POST",
datatype:"json",
url: "/notification.php",
data: {
kime=karaakce,
tur=konuyayorum,
postid=2020
}
success: function(responseData){
$.ajax({
type: "POST",
datatype:"json",
url: "/track.php",
data: {
kime=karaakce,
tur=konuyayorum,
postid=2020
}
success: function(html){
// This is your final success
}
});
}
});
Your notification.php file should return a JSON data which you can use to send it to the next request. It will come in 'responseData' object.
I have script using smarty engine.
I just tried to use ajax post to get some data from page.
The javascript function as following:
function getTime(val,id) {
$.ajax({
type: "POST",
url: '{$GLOBALS.site_url}/check_time/',
data:'GetDate='+val + '&Jobid='+id,
success: function(data){
alert(data);
}
});
}
the alert show me (3 small squares) I don't know what dose mean !
I'm beginning web development with php and am testing jQuery ajax calls to a php file to run functions. But I've noticed that the php file loads into the resources each time I call it with an AJAX POST method. What is the best solution to prevent this occurrence? Also, are there better coding practices to use when performing multiple function calls (what I'm used to calling web services or web methods in the c# world) from a single file in php?
test.php
<?php
if($_POST['action']=='test'){
$arr = array(
'stack'=>'overflow',
'key'=>'value'
);
echo json_encode($arr);
}
?>
scripts.js
function postTest(){
var data = {
action: 'test'
};
$.ajax({
type: "POST",
url: "test.php",
dataType: 'json',
data: data,
success: function(result){
console.log(result)
}
});
}
Update:
I changed my code to use the data variable as an object in the ajax call.
The original question still stands however. How do I use a function inside a php file without it being loaded into the site resources in the browser for each ajax call?
Thank you.
If you're noticing that the php block is never getting executed, then you're passing in the data wrong. Try this:
function postTest(){
var data = {
'action': 'test'
};
$.ajax({
type: "POST",
url: "test.php",
dataType: 'json',
data: data,
success: function(result){
console.log(result)
}
});
}
JSON.stringify is not working in your favor, here.
To demonstrate this, you could put print_r($_POST) in your php script and see what console.log(result) gives you.
To remedy this, you can simply put your data in the $.ajax() block:
function postTest(){
$.ajax({
type: "POST",
url: "test.php",
dataType: 'json',
data: {
action: 'test'
},
success: function(result){
console.log(result)
}
});
}
I have links of messages coming in, say in an email message body. The issue right now is I want to click on the content and be able to display the unwrapped link in the right message pane (I use jQuery).
I am using jQuery and figured that using AJAX to call another PHP script to send the requests to bit.ly, etc. would work. I am not 100% sure how to do this.
The main issue is that some of these links are 2-3 times shortened as Twitter has their own automatic shortner. Any help on this would be appreciated.
Thanks.
You could use a service called LongURL. The API call in jQuery would look something like this:
$.ajax({
url: 'http://api.longurl.org/v2/expand?format=json&url=http%3A%2F%2Fbit.ly%2Fv20RLs',
dataType: 'jsonp',
method: 'get',
success: function(response) {
alert(response['long-url']);
}
});
or
$.ajax({
url: 'http://api.longurl.org/v2/expand',
data: {
format: 'json',
url: 'http://bit.ly/v20RLs'
},
dataType: 'jsonp',
method: 'get',
success: function(response) {
alert(response['long-url']);
}
});