Make nginx return a JSON header



PHP/5.4.45, nginx/1.2.1




My PHP script set correct content/type for json:



header('Content-Type: application/json; charset=UTF-8');


But in server response header:



Content-Type:text/html


How I should change header for this php page?



header function work correctly on apache servers. How to set
correct header on nginx?

I have this data:



{
"_id" : ObjectId("5461e16ee7caf96f8f3584a2"),
"num_marcacao" : "100",
"sexo" : "Fêmea",
"idade" : "20",
"bigdata" : {
"abortos" : [
{
"data_aborto" : "2014-11-11",
"causa_aborto" : "Aborto causa 1"
},
{
"data_aborto" : "2014-09-01",
"causa_aborto" : "Aborto causa 2"
}
],
"crias" : [
ObjectId("5461e16ee7caf96f8f3584a2")
]
}
}
{
"_id" : ObjectId("5461e1cae7caf96f8f3584a4"),
"num_marcacao" : "200",
"sexo" : "Fêmea",
"bigdata" : {
"crias" : [
ObjectId("5461e1f3e7caf96f8f3584a5"),
ObjectId("5461e760e7caf96f8f3584a6")
]
}
}


Using the following distinct function I get one result



db.animal.distinct('_id', {'bigdata.crias':{$exists:true}}


Result:



{
"0" : ObjectId("5461e16ee7caf96f8f3584a2"),
"1" : ObjectId("5461e1cae7caf96f8f3584a4")
}


Now I want to get the array that is in bigdata.crias
like the result of the distinct query.
I'm trying to do like this:



db.animal.find(
{
$and: [
{'num_marcacao': '200'},
{'bigdata.crias':{$exists: true}}
]
},
{
'bigdata.crias': true,
'_id': false
}
)


But the result is not like the one I need. This is what it's
returning:



{
"bigdata" : {
"crias" : [
ObjectId("5461e1f3e7caf96f8f3584a5"),
ObjectId("5461e760e7caf96f8f3584a6")
]
}
}


And I need



{
"0" : ObjectId("5461e1f3e7caf96f8f3584a5"),
"1" : ObjectId("5461e760e7caf96f8f3584a6")
}
Web Development

Hello i am working with MVC and Ajax and jquery.I am returning my
json value from my action method like below.



   public ActionResult GetMYSDRViews(List<string>
AssetNames, List<string> UtilizationHubs, string RedirectView)
{



try
{
//ViewBag.InitParams = MvcApplication.objInitParams;
string UserType = MvcApplication.objInitParams.UserType;


bool Execute;
List<MYSDR> MysdrListCollection = new
List<MYSDR>();
ServiceClient objService = new ServiceClient();
objService.ClientCredentials.Windows.ClientCredential =
new System.Net.NetworkCredential("v-saramb", "sdatnov@2014",
"REDMOND");


objService.ClientCredentials.Windows.AllowedImpersonationLevel =
System.Security.Principal.TokenImpersonationLevel.Impersonation;

var result = String.Empty;

if (RedirectView == "Agent View")
{
ViewData["Flag"] = "AgentView";
#region Agent View
result =
objService.SalesDeskAgent_DynamicAsset("REDMOND\v-susudh", "", "");
#endregion
}
else if (RedirectView == "Manager View")
{
//result =
objService.SalesDeskAgent_DynamicAsset("REDMOND\v-susudh", "", "");

ViewData["Flag"] = "Manager View";
userHUbdata.Clear();
foreach (string Hub in UtilizationHubs)
{
ServiceReference.MYSDRFields obj2 = new
ServiceReference.MYSDRFields();
obj2.Hub = Hub;//cmbHubs.SelectedItem.ToString();
if (obj2.Hub != "All")
userHUbdata.Add(obj2);
}


result =
objService.SalesDeskManagerView_ConditionbasedwithFilters("", "",
userHUbdata, true);
}
//else if (RedirectView == "BulkAssignment View")
// {
// result =
objService.SalesDeskManagerView("REDMOND\v-susudh", "");
// }


XDocument xmldoc = null;
try
{
xmldoc = XDocument.Parse(result.ToString());
Execute = true;
}
catch
{
Execute = false;
}
if (Execute)
{
#region
MysdrListCollection.Clear();


var data = (from info in
xmldoc.Elements("NewDataSet").Elements("Table1")
where
Convert.ToString(info.Element("Status").Value) != "" &&
Convert.ToString(info.Element("Status").Value) != "New" &&
Convert.ToString(info.Element("Status").Value) != "Projected"
select new MYSDR
{
SRNO =
Convert.ToString(info.Element("SRNO").Value),
CustomerName =
Convert.ToString(info.Element("CustomerName0").Value),
ServiceLevel =
Convert.ToString(info.Element("TypeofRequest").Value.Replace("Unsolicited",
"Proactive").Replace("Partner Proactive", "Proactive")),
CRMID =
Convert.ToString(info.Element("CRMID").Value),
//DueDate_Date =
Convert.ToDateTime(info.Element("DueDate").Value),
RequestType =
((Convert.ToString(info.Element("TypeofRequest").Value) == "Managed
Proposal") || (Convert.ToString(info.Element("TypeofRequest").Value)
== "Standard Proposal") ||
(Convert.ToString(info.Element("TypeofRequest").Value) == "Unsolicited
Proposal") || (Convert.ToString(info.Element("TypeofRequest").Value)
== "Unsolicited Proposal Lite")) ? "Proposal" :
((Convert.ToString(info.Element("TypeofRequest").Value) == "AIO") ||
(Convert.ToString(info.Element("TypeofRequest").Value) == "AIO Prep"))
? "AIO" : ((Convert.ToString(info.Element("TypeofRequest").Value) ==
"M&A Full") ||
(Convert.ToString(info.Element("TypeofRequest").Value) == "M&A
Light")) ? "M&A" :
Convert.ToString(info.Element("TypeofRequest").Value),
Region =
Convert.ToString(info.Element("Region").Value),
Segment =
Convert.ToString(info.Element("Segment").Value),
Assigned =
((Convert.ToString(info.Element("Status").Value) == "Projected") ||
((Convert.ToString(info.Element("Status").Value) == "New"))) ?
"Assign" : Convert.ToString(info.Element("AssignedToName").Value),
Language =
Convert.ToString(info.Element("Language").Value),
ID =
Convert.ToString(info.Element("ID").Value),
Status =
Convert.ToString(info.Element("Status").Value),
AM =
Convert.ToString(info.Element("AM").Value),
Hub =
Convert.ToString(info.Element("Hub").Value),
DueDate =
Convert.ToDateTime(info.Element("DueDate").Value).Date,
RequestDate =
Convert.ToDateTime(info.Element("Requestdate").Value).Date,
PrimaryContact =
Convert.ToString(info.Element("PrimaryContact").Value),
Country =
Convert.ToString(info.Element("Country").Value),
SolutionForProposal =
Convert.ToString(info.Element("SolutionForProposal").Value),
Reactive =
((Convert.ToString(info.Element("TypeofRequest").Value).Contains("Research"))
&& (Convert.ToString(info.Element("RequestOrigin").Value) ==
"Reactive")) ? "R" : "",
CoOwner =
((Convert.ToString(info.Element("Status").Value) == "New") ||
(Convert.ToString(info.Element("Status").Value) == "Projected") ||
(Convert.ToString(info.Element("TypeofRequest").Value).StartsWith("QA")))
? "" : (((Convert.ToString(info.Element("Status").Value) != "New") ||
(Convert.ToString(info.Element("Status").Value) != "Projected"))
&& (Convert.ToString(info.Element("Coowner").Value) == "")) ?
"Add Co-Owner" : (Convert.ToString(info.Element("Coowner").Value)),

}).AsEnumerable();
MysdrListCollection = data.ToList<MYSDR>();
// Session["MYSDRGridAgentView"] =
MysdrListCollection.OrderBy(a => a.DueDate);
// return View(data);
#endregion

}

var serializer = new JavaScriptSerializer();
var resultContent = new ContentResult();
serializer.MaxJsonLength = Int32.MaxValue; // Whatever max
length you want here
resultContent.Content =
serializer.Serialize(MysdrListCollection.OrderBy(a => a.DueDate));
resultContent.ContentType = "application/json";
return resultContent;
// return Json(MysdrListCollection.OrderBy(a =>
a.DueDate),JsonRequestBehavior.AllowGet);
// return View("Mysdrviews",MysdrListCollection.OrderBy(a
=> a.DueDate));
//return MysdrListCollection;



}




catch (Exception ex)
{
return Json("");
}





}


I am using my ajax call from jquery like below.



 $.ajax({
url: '@Url.Action("GetMYSDRViews", "MYSDR")',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
type: 'POST',
// url: '/MYSDR/PostbtnSubmit',
data: JSON.stringify({ AssetNames: itemsListAssets,
UtilizationHubs: itemUtilizationHub, RedirectView: 'Manager View' }),

success: function (result) {
debugger;
var grid = $('#MYSDRGridAgentView').getKendoGrid();
grid.dataSource.data(result);
grid.refresh();
$('#Loader').oLoader('hide');
// $('#result').html('"PassThings()" successfully
called.');
},
failure: function (response) {
debugger;
// $('#result').html(response);
}
});


Up to here everything is fine.But when i try to return my json
result in my return view method like below i am not getting any error
but i am not able to successfully return result and my debugger pint
also not hitting at Success or Error
functions in my jquery.



Please let me know where to change my Ajax function..I am stuck
here like anything....

Web Development

I'm writing a test to check for invalid DELETE requests to a Rails
API using Rspec.



This is what I have:



context 'invalid id number' do
it 'returns success: false' do
xhr :delete, :destroy, id: 999999999999999999
expect(JSON.parse(response.body)['success']).to be_false
end
end


Postgres throws some kind of integer overflow exception (as it
should), but in my spec I can't look at the JSON object because it's
never formed. How can I make it return { success : false } instead of
a blank string? How do I force the JSON object to return despite the
exception?

Web Development

I am comfuse about the question for some days。
when I send the request to the tomcat server ,it always get the
right request。but when I put nginx server proxy pass the tomcat
server,that the nginx cut off the request body。
here is my nginx settings。



location /abc {
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:8080;
keepalive_timeout 0;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
proxy_read_timeout 900s;
proxy_send_timeout 900s;
}


nginx version is 1.6.0



and the nginx access log shows



10.87.128.146 - - [13/Jan/2015:12:30:33 +0800] "GET
/abc/12.do?ct=abcdefg&gi=cd&te=ef&sa=gh&v=V1 HTTP/1.1"
200 136 16 "-" "-" "-"
10.87.128.146 - - [13/Jan/2015:12:30:33 +0800] " /abc/12.do?gi=a" 400
0 166 "-" "-" "-"
10.87.128.146 - - [13/Jan/2015:12:30:33 +0800]
"bcdefg&p=ab&ss=c" 400 0 166 "-" "-" "-"
10.87.128.146 - - [13/Jan/2015:12:30:33 +0800] "=V1 HTTP/1.1" 400 0
166 "-" "-" "-"
10.87.128.146 - - [13/Jan/2015:12:30:33 +0800] "25:80" 400 0 166 "-"
"-" "-"
10.87.128.146 - - [13/Jan/2015:12:30:33 +0800] "ction: Keep-Ali" 400 0
166 "-" "-" "-"
10.87.128.146 - - [13/Jan/2015:12:30:33 +0800] "ve" 400 0 166 "-" "-"
"-"
10.87.128.146 - - [13/Jan/2015:12:30:35 +0800] "GET
/abc/12.do?gi=abcdefg&di=ef&p=ab&ss=c&ct=aa&ld=bb&v=1
HTTP/1.1" 200 148 16 "-" "-" "-"
Web Development

I have built a page showing a number of items with their thumbnail
images. The page has a pager and the user can iterate over the results
back and forth. Each iteration request is made using AJAX and a JSON
response is returned. Currently the JSON response also includes the
Base64 encoded thumbnail image for each item. For 12 items the
response is about 100KB(gzip encoded). Thumbnails are compressed as
JPEG.



Is this a good way? Instead of embedding thumbnails in the JSON
response, is it preferrable to just include URLs to thumbnail images?



Please answer in terms of performance(and maybe bandwidth) both
client side and server side.

Web Development

PHP/5.4.45, nginx/1.2.1




My PHP script set correct content/type for json:



header('Content-Type: application/json; charset=UTF-8');


But in server response header:



Content-Type:text/html


How I should change header for this php page?



header function work correctly on apache servers. How to set
correct header on nginx?

Network & Servers

- Technology - Languages
+ Webmasters
+ Development
+ Development Tools
+ Internet
+ Mobile Programming
+ Linux
+ Unix
+ Apple
+ Ubuntu
+ Mobile & Tablets
+ Databases
+ Android
+ Network & Servers
+ Operating Systems
+ Coding
+ Design Software
+ Web Development
+ Game Development
+ Access
+ Excel
+ Web Design
+ Web Hosting
+ Web Site Reviews
+ Domain Name
+ Information Security
+ Software
+ Computers
+ Electronics
+ Hardware
+ Windows
+ PHP
+ ASP/ASP.Net
+ C/C++/C#
+ VB/VB.Net
+ JAVA
+ Javascript
+ Programming
Privacy Policy - Copyrights Notice - Feedback - Report Violation 2018 © BigHow