Handle json array CAKEPHP - json

Here's my javascript snipet
<script type="text/javascript">
$(document).ready(function() {
$('form#FormId button.btn').click(function(event){
$.ajax({
type: "POST",
url: "/controller/edit",
data: $("#FormId").serialize(),
success: function(response) {
alert(response.message);
alert(response['message']);
}
});
});
});
Here's my controller action
public function edit() {
$this->autoRender = false; // We don't render a view in this example
$this->request->onlyAllow('ajax'); // No direct access via browser URL
echo json_encode(array('message'=>'Welcome','type'=>'success'));
exit;
}
Both alerts on the javascript are returning "undefined" how to handle?

So nobody is getting this correct.
You need to use the JSON view
See how to enable with this section
class PostsController extends AppController {
public $components = array('RequestHandler');
public function index() {
$this->request->onlyAllow('ajax');
$this->set(array(
'data' => array('message'=>'Welcome','type'=>'success'),
'_serialize' => 'data',
));
}
}
Cake will now automatically set the correct headers, serialize as JSON and not render the layout.
As a side note, your code alert(response.message); does not work beacuse response is a string. your header is text/html not application/json. Try console.log(response) and you will see it is just a string.

I get this to work using the following code in the controller
public function edit()
{
$this->RequestHandler->respondAs('json'); // Very important without this it will not work
$this->autoRender = false;
$data = array('message'=>'Welcome','type'=>'success');
return json_encode($data);
}

try add
dataType: 'json',
to your ajax method.
if it not work, try this:
add
$this->response->type('text/plain');
to your index method.
ie brower did not know json format text.
and what broswer did you used for this test? use F12 open the developer kit to check what the server response, is it a objct

From my experience echoing content doesn't work. You should instead return the json_encoded data.
This will make your edit() function look like this:
public function edit()
{
$this->autoRender = false;
$this->request->onlyAllow('ajax');
return json_encode(array('message' => 'Welcome', 'type' => 'success'));
}
To make sure it works; just alert the whole response object to see what is returned.

Related

json_encode generates a very long string when I encode my table. So far my app works but will the string be too long eventually?

The string is over 612,000 characters long. I'm using a Laravel framework to generate markers on a google map so they all have to be loaded. json_encode is a very convenient way to do it but the string is ridiculously long.
Within a Laravel framework, instead of using json_encode I used ajax to retrieve the data so that it didn't get exposed to the browser.
blade file snippit:
function initMap() {
// Ajax call to load entire database to a Jquery array
AjaxGet = function () {
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $("meta[name='csrf-token']").attr('content')
}
});
var result = $.ajax({
url: "{{url('/ajaxgetter')}}",
method: 'post',
async: false, //obsolete flag, but necessary so that the array does not try to set before data is retrieved
success: function (result) {}
}).responseText;
return result;
}
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
class AjaxController extends Controller
{
public function index () {return View('ajax');}
public function ajaxgetter() {
return response()->json(DB::select('select * from workorders'), 200);
}
}

How to call a controller directly using ajax

Let say I have 2 project ClientSide and ServerSide. I need to call a function in a controller of ServerSide from ClientSide. Can it be done directly using Ajax?
This is my Ajax code (it will work if the controller is put in the same project):
var url = 'api/values/insert';
$.ajax({
type: method,
url: url,
data: JSON.stringify(data),
contentType: 'application/json',
success: function (data) {
window.location.href = "/index";
}
});
The local host for my ClientSide is https://localhost:44356, and the one for my ServerSide is https://localhost:44329. I have tried adding the local host into the url but it's still not working.
var url = 'https://localhost:44329/api/values/insert';
Is there any other method could help me solve this problem? I am appreciated for all the suggestion and answers.
For more specific information if needed, I am using .NET 2.1
UPDATE:
This is my controller code in ServerSide. If I put the controller in ClientSide, it works without any modification needed, so I believe the problem is about the connection between Ajax and the controller.
namespace RazorAPI.Controllers
{
[Produces("application/json")]
[Route("api/[controller]")]
public class ValuesController : Controller
{
private readonly IDataService _dataService;
public ValuesController(IDataService dataService)
{
_dataService = dataService;
}
// POST: api/values/insert
[HttpPost]
[Route("[action]")]
public void Insert([FromBody]Data data)
{
//This call another function in ServerSide
_dataService.Create(data);
}
}
After working around with it, I found out the problem is because of CORS issue. This is how I fix it.
In Startup.cs file of ServerSide project, add the following code to Configure method:
app.UseCors("AllowAllHeaders");
Add the following code to ConfigureService method:
services.AddCors(options =>
{
options.AddPolicy("AllowAllHeaders",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
Now use the url include local host to ServerSide:
var url = 'https://localhost:44329/api/values/insert';
This works for me but it will allow acess from any domain. If you need it to be only accessed by specific origin, change the code in Configure into:
app.UseCors("AllowSpecificOrigin");
And for the code in ConfigureService:
services.AddCors(options =>
{
options.AddPolicy("AllowSpecificOrigin",
builder =>
{
builder.WithOrigins("localhost")
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod();
});
});
Thank you for all the help!

MVC5 action that can return either View(model) or json

I have a following problem:
I got an EDIT page, it contains some inputs and so on - nothing unusual. On submit button click I have to validate the form on server and do one of the following actions:
return the model if there are errors
return some info saying it's all good
Why? Because I need to display a bootstrap modal window when everything's ok. So what I did is (simplifying):
[HttpPost]
public ActionResult EditData(DataViewModel model)
{
...
if(!ModelState.IsValid)
return View(model);
return Json(new { success = true, message = "all good" }, JsonRequestBehavior.AllowGet);
}
What is the problem? I'm posting the form with javascript and waiting for the answer like that:
{
$(document).on('click', '#submitButton', function () {
$.ajax({
dataType: "json",
url: "/Controller/EditData",
type: "POST",
data: $('#submitForm').serialize()
}).done(function (data) {
console.log(data.success + ' ' + data.message);
if (data.success) {
$('#modalBody').text(data.message);
$('#modal-window-add').modal('show');
}
});
});
}
So as you can see it does display the modal window but it doesn't do anything when a model comes in. In the latter situation completely nothing happens although the response comes.
Is there any other option to solve my problem? Generally I HAVE to display that modal window on successful edit and on modal closing I need to redirect to another page - this is easy. I tried including the model inside the json but it didn't work and it seems like a lot of work to check all the fields manually and then displaying these red errors under inputs that didn't pass validation. I discussed it with my experienced friends but they don't have any MVC based solution. Any idea?
Best regards,
Daniel
User Ajax For to do that work
#{
var ajaxOptions = new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "modal", OnBegin = "onAjaxBegin", OnComplete = "OnAjaxComplete", OnSuccess = "OnAjaxSuccess" };
}
#using (Ajax.BeginForm("Controler", "EditData", ajaxOptions, new { enctype = "multipart/form-data" }))
{
//write html here with model values
}
COntroller Method
[HttpPost]
public ActionResult EditData(DataViewModel model)
{
if(!ModelState.IsValid)
return View(model);
}
Javascript
function OnAjaxSuccess()
{
show hide model popup
}
$.ajax have a dataType option (The type of data that you're expecting back from the server.)
In $.ajax function you added option dataType: "json" that means you expected json back from server so you will get only json not other dataType like xml, html, text, script
In your case you want to return json or html depending on model is valid or not for that you can remove dateType option from $.ajax function, because If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string).

Pass a value from controller as JSON in codeigniter to view that contain on Angular Js

I need to use this:
http://demos.telerik.com/kendo-ui/multiselect/angular
but my problem when I try to change the url to:
read: {
url: base_url+"index.php/welcome/findAll",
}
and the function findAll in controller contain on:
public function findAll()
{
$listproduct = $this->mproduct->findAll();
echo json_encode($listproduct);
}
The list of options become empty.
You controller seems to be fine as long as it returns a valid JSON format.. in your AngularJS controller try doing:
$scope.MyFunction = function()
{
console.log("posting data....");
$http({
method : 'GET',
url : 'http://domain/controller/method_that_return_json',
headers: {'Content-Type': 'application/json'},
data : JSON.stringify({What you want to return from JSON here})
}).success(function(data) {
console.log(data);
});
}
Another aproach:
$http.get("http://www.domain.com/pathtojson)
.success(function(response) {
$scope.related = response;
});
}

ASP.NET MVC Show View after Ajax call to a Controller

I have a View with a submit form: when I click it a jquery/ajax function is called. This function has to encode the View Model, call a Controller action and show the View returned.
Now, this is my function:
<script type="text/javascript">
function Analyze() {
var urlact = '#Url.Action("Analysis")';
var model = '#Html.Raw(Json.Encode(Model))';
$.ajax({
data: model,
type: "POST",
url: urlact,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
//WHAT HERE??
}
});
}
</script>
And the Analysis action is a kind of
public ViewResult Analysis(IEnumerable<Azienda> aziende) {
Debug.WriteLine(aziende.Count());
return View(aziende);
}
Returning a View!
How can I display that View on success:function(data)?
I tried changing dataType to html and calling on success alert(data) but I had problems with the encoded model, I tried commenting the contentType line but same model-encoding issue.
Does someone know how to do that?
A js/query/ajax workaround-trick is fine, too.
Thanks you all!
Use
return PartialView()
instead of
return View()
in your controller. Then in the success function in your ajax call, use the jQuery .html() function to update the element you wish to update in your html. See Query.html() and View() vs. PartialView()
Create a separate partial view with aziende as its model and return this in your Analysis action and then append the result to a div in your view:
//action returning a partial view
public ActionResult Analysis(IEnumerable<Azienda> aziende)
{
Debug.WriteLine(aziende.Count());
return PartialView("_partialView", aziende);
}
//then append the result to a div in your javascript
success: function (data) {
$("#some-div").html(data);
}