When i submit a FORM on my page, my "response text" returns the HTML of the entire page and not only the FORM that submitted. This is normal?
responseText is XHR speak for "What the server responds to the request with, excluding the HTTP headers".
It is perfectly normal for it to include a complete HTML document if you access a resource designed to respond to a regular form submission.
It sounds like you need to be smarter about what the server responds with.
This simple example (written in Perl, see the link for more context) checks a query string parameter to decide if it should place the data it has fetched in an HTML template and return it, or to convert the data to JSON and return that instead.
if ($view eq "json") {
my $data = $json->convert_blessed->encode($vars);
print $q->header('application/json;charset=utf-8'), $data;
return;
}
my $output;
$tt->process('html.tt', $vars, \$output)
|| die $tt->error(), "\n";
print $q->header('text/html;charset=utf-8'), $output;
Related
The instructions in clickfunnels as me to start by creating a test endpoint URL:
. Creating A Test Endpoint.
First, you will need to create a test endpoint at <your-domain/funnel_webhooks/test>
And it should include the headers below.
Content-Type as application/json
X-Clickfunnels-Webhook-Delivery-Id as an MD5 of the URL and Payload.
The payload (HTTP message body) will be a JSON object with a key of "time" and value of the current time in UTC as follows:
{ "time": "YYYY-MM-DD HH:MM:SS UTC" }
I went into the file manager in my hosting and added a folder for funnel_webhooks & a file called test.
I think I changed the content type to JSON
And, I think I figured out how to create the JSON object within the file.
I'm not sure how to do this part:
X-Clickfunnels-Webhook-Delivery-Id as an MD5 of the URL and Payload.
This loom video will show where I'm at so far in the process.
https://www.loom.com/share/1c9be96014b8413a8c9ba54f56dd42a8
Any support would be greatly appreciated!
It's actually much more simple than you think. They just want to verify you own the domain by getting a 200 response from any file you place in the folder on your server.
Create this folder structure on your server in the root and add an index.php file or index.html file like this:
/funnel_webhooks/test/index.php (I wrote "hello" in this file for good measure)
Go to Click Funnels. Click your Funnel. Click Settings. Scroll to bottom and click Manage Your Funnel Webhooks
Create a New webhook with the URL: whateveryourdomain/funnel_webhooks/test/index.php
Click Create Funnel Webhook Button
If your file is in the right location then click funnels will use this to verify you own the domain.
All of your webhooks going forward will work with no problem as long as you use this domain that was verified.
let me give it a try!
What you are doing right now is just creating a JSON file that is not going to get the webhook event from the clickfunnel.
Start by creating a PHP file if your server is compatible with PHP.
And in it just write this code and try it out.
<?php
//set the headers.
header("Content-Type: application/json;");
header("X-Clickfunnels-Webhook-Delivery-Id : MD5 of CONTENTS HERE");
//check if there is input data.
if ($json = json_decode(file_get_contents("php://input"), true)) {
//print and set the data as variable
print_r($json);
$data = $json;
}else{
//try getting the POST data.
print_r($_POST);
$data = $_POST;
}
//set the response as OK - 200
http_response_code(200); //response code for OK.
echo json_encode(array("status" => 'OK', "code" => 1, "payload" => $data));
//save the data as a file to check the information that was sent by the webhook.
$file = 'webhook_contents.txt';
$current = file_get_contents($file);
$current .= date("Y-m-d h:i:s").json_encode($data);
file_put_contents($file, $current);
// From here, it all depends on what you want to do with the webhook event data.
?>
this code should give you the Headers to get a JSON payload, and echo a response of said payload.
At the same time it should create a file with the name "webhook_contents.txt" in which you should be able to read the webhook event payload.
Let me know if it works!
So I have a wordpress Ajax function that retrieves MySQL data as JSON and logs it. However I want to not directly get the data back on the page where my AJAX function is, but I want to save the data to a JSON file instead, so I can use it for a wider variety of purposes.
Here is my AJAX function:
$.get(
ajax_url,
data,
function(data) { // AJAX callback
fill_json(data);
}// End AJAX callback
);
The fill_json() is a function to echo the JSON data in a table I wrote myself.
Now here is what happens inside my AJAX hook:
$sql_search = $wpdb->get_results(" a complicated mysql search here ");
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$result = json_encode($result);
echo $result;
} else {
header("Location: ".$_SERVER["HTTP_REFERER"]);
}
underneath echo $result; in my Ajax hook I tried the following piece of code, but I don't know how I can see if it worked or not:
$json_path = "/var/www/vhosts/jtc.ae/httpdocs/pre/wp/wp-content/themes/Amazing_japan_HP/new/search.json";
file_put_contents($json_path, $result);
My question:
Is this the correct way to save the data to a JSON file, and how can I get this data on my main page then?
Extra question: Will saving $result to a JSON file conflict with multiple users using the AJAX at the same time?
All you did is right .. please make sure the steps below :
Pass write permission to the folder /new inside "Amazing_japan_HP" (chmod -R 666 /new)
To save the data in file use file_put_contents($file, $current);
To get the data from the file use $current = file_get_contents($file);
For all newly created files try to use umask. It provides required permission to the files for writing content in it
umask() sets PHP's umask to mask & 0777 and returns the old umask.
When PHP is being used as a server module, the umask is restored when
each request is finished.
I have a controller that uploads and processes a file. Afterwards, I wish to render the processing result in a modal div. I wanted to know what the best way is to get the results from the controller to the modal div on the gsp. I thought about a template but I didn't know how to specify what the target div for the template should be because this template wouldn't be rendered by a button click where a target for template render is set as an attribute, it would be done on a timed basis (i.e. when the file is done uploading). The other way is to send JSON back from the controller but I don't know how to intercept this JSON at the right time because I still don't quite understand the timings of the information flow between the GSP and the Controller. I know how to send the JSON but how to alert the GSP that "hey, some JSON is now ready for your modal that's about to go up." Here is some pseoducode of basically what I am trying to get done.
Controller:
upload() {
// process file and store results in three integers
// int1 = result1
// int2 = result2
// int3 = result3
// send the three numbers to the gsp
}
Now what is the best way to get these three numbers to the GSP so that they are displayed on a modal dialog which is about to go up like this:
<div id="fileUploadResultsModal">
Results:
${int1}, ${int2}, ${int3}
</div>
Here is the JS associated with my ajax upload function:
$("#chseFile").upload("${createLink(controller: 'customer', action: 'upload',)}",
{dataTypegrp: parseInt(getCheckedValue(document.getElementsByName('dataTypegrp'))),
fileTypegrp: parseInt(getCheckedValue(document.getElementsByName('fileTypegrp')))},
function(success) {
$("#cancel1").trigger("click");
setTimeout(function(){
$("#summary").trigger("click");
}, 250);
displaySuccess(data);
},
function(prog, value) {
console.log(value);
$("#prog").val(value);
if (value == 100) {
$("#prog").hide();
$("#progressbar").html("Uploading and processing. Please wait...");
}
});
but right now JS complains that 'data' is not defined. 'data' is meant to be the JSON coming back from the controller.
Thanks
you can render them as JSON:
render( [ int1:111, int2:222, int3:333 ] as JSON )
or as a HTML-string
render "<div id=\"fileUploadResultsModal\">Results:${int1}, ${int2}, ${int3}</div>"
or use a template
render template:'/yourController/templateName', model:[ int1:111, int2:222, int3:333 ]
or a TagLib
render g.yourResultTag( int1:111, int2:222, int3:333 )
For this tiny bit of information, the performance is not of concern. It's rather a matter of taste, or what is more appropriate for your client.
If the later is JSON-biased, use JSON-rendering. If it has a mix of JSON and HTML, use others.
inside controller at the enf of controller action you can use
render [data:['name':'firstname','surname':'secondName'] as JSON]
this will render the data to GSP
I am using a predefined response format for all my ajax calls.
If the request is success then the server will respond :
{"status":true,"data":{"name":"person","age":2}}
Please note data is not necessary.
and if the request failed then i will get
{"status":false,"reason":"You are not authorised."}
SO every response have a status field , if status is FALSE then there will be reason field.
The problem is that now i enables CSRF protection in Codeigniter and if the token expired/failed the system outputs
The action you have requested is not allowed.
this is HTML content.
Is it possible to extend the security class ,so that if the request is through Ajax then it will keep json_encoded format else use the html format.(i do not want to modify the core)
Thanks.
This error message is rendered from the CI_Exceptions::show_error method (located in system/core). You can extend this class by the usual way and override this method in order to catch this error and return whatever you want.
You can get rid of the call inside the CI_Security::csrf_show_error method by overriding it so it won't simply call
show_error('The action you have requested is not allowed.');
This is probably more robust.
Alternatively you can attack this inside CI_Exceptions class. Since this errors doesn't come with specific error code you will have to match for the message which could break between updates (currently hardcoded). The resulting class could look like this:
class MY_Exceptions extends CI_Exceptions {
public function show_error($heading, $message, $template = 'error_general', $status_code = 500) {
if ($message == 'The action you have requested is not allowed.') {
// handle this error your way
} else {
// send every other error to the original handler
parent::show_error($heading, $message, $template, $status_code);
}
}
}
I am trying to save HTML content with the Symfony2 form system but I am facing issues with escaping.
My addPost action looks like this
$em = $this->getDoctrine()->getEntityManager();
$post = new Post();
$postForm = $this->createForm(new PostFormType(), $post);
if ($request->getMethod() == 'POST') {
$postForm->bindRequest($request);
if($postForm->isValid()){
$em->persist($post);
$em->flush();
}
}
The problem is that the post form has a content textarea that allows the user to enter html. When the form is submitted with html like test the content gets saved to the database as test. And then on each subsequent save the backslashes escape themselves again and again...
What is the proper way to store HTML with the Symfony2 form component?
You should remove the backslashes from the string before sending it to the user to edit. You should be able to get the content from the $post object and then strip its slashes and set it back to the post content like so.
$post = new Post();
$post->setContent(stripslashes($post->getContent()));
$postForm = $this->createForm(new PostFormType(), $post);
Now when the form renders it will not have the escape characters.
However, I have found that if you return a response to the same page you will still see the backslashes. To avoid this you can return a RedirectResponse like this
if($postForm->isValid()){
$em->flush();
return new RedirectResponse('route', array('postId' => $postId)));
}
Those slashes are placed there so that the mysql engine doesn't confuse those quotes with sql syntax. I suggest you use a rejex to get rid of all the slashes when you retrieve the field from the database, but before you post it to the client. let me see if I can find the rejex for you, maybe someone will beat me to it :)