Problem in fetch of a json server in react - reactjs

VM543:1 Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON
I have problem at fetch of json server that i made alone .The server is correct

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.
You are trying to parse json from your response (string). something like JSON.parse('<!doctype html...')
Your server is not returning an application/json data

Related

I need to access data in the object message: "{'data':{'status':'active'}}". it throws error while access while stringfy

message: "{'data':{'status':'active'}}"
this is my object i need to access data in the json.i unable to access this.if i tried to access the data, it gives json access error.
JSON.parse() does not allow single quotes.
So if you want to parse "{'data':{'status':'active'}}" then you have to first replace all the single quotes with double quotes
const object = { message: "{'data':{'status':'active'}}"}
const parsedObject = JSON.parse(object.message.replaceAll("'", '"'))
console.log(parsedObject)
JSON.parse

How can i get Json Data if its having in particular Name

Here im having data Like
""{\"resultStatus\":\"success\",\"message\":\"AUTH-040010: Successfully logout user\",\"language\":\"en\",\"region\":\"us\",\"securityToken\":\"C29F32A5-CF46-4CE8-B1DF-4FBC207ABA19|13|1550120060454\",\"userName\":\"John\",\"organisationId\":1,\"isSearch\":false,\"productId\":1,\"productKey\":\"fAyhhy455Hh4d52c\",\"menuKey\":\"LOG\",\"pageDto\":{\"selectedPage\":1,\"totalCount\":0,\"recodsPerPage\":25},\"menuMasterDtoList\":{},\"validated\":true}Redirectdata=https://192.168.10.19:8089/iauth/access/login?productId=1&productKey=f8cc80d…a24d52c&redirectUrl=http://172.16.1.135:458/Enquiry/openenquiries&Message= Success&MessageType=1""
in this Url How can i get Redirectdata Url
JSON.stringify(d.data) By using this i convert that string in Json
But
JSON.stringify(d.data.Redirectdata)
This is Giving me undefind
Please help me how can i get Redirectdata Url
It because Redirectdata is not a JSON key its outside of JSON object

IE - Angular's json response contains unicode character that removed additional characters. Invalid JSON

I have a http response that has a name that contains a unicode character (ex. Müller).
In IE11, I'm getting an error that says "Invalid Character" because it seems that in IE11 the whole http response is read as a string in angular's http response, and it tries to parse this string into JSON (instead of already in JSON format). But in the JSON string it looks something like this:
...,\"lastName\":\"M�}],\"id\":1,...
The problem is that part of the last name got stripped, and now the lastName value has a missing close quote. I don't mind that its displaying the diamond question mark, its just completely breaking the response now.
In chrome it works fine, as the data is actually returned as a JSON object, unlike IE11 where it returned as a string, and then tries to convert to JSON in the default transform response functions.
The request is in application/json charset: utf-8 format.
The response is in application/json format.
Does anyone know whats wrong?
Edit: In IE11's network response body, it shows it correctly as "Müller" in the JSON format.
Edit: It seems like its eating up the first 5 characters after the ü when returning the response. (ex. Mülleraa will look like ...\"M�a\"... where the closing quote is back with an additional 'a' char)
In your request, add:
headers: {
"Accept": "application/json;charset=utf-8",
"Accept-Charset":"charset=utf-8"
},

How to pass a list value to a webapp2 backend via $.post?

In my coffeescript frontend I attempt to pass a list of value to the backend
data = {
id: [2, 3, 4]
}
$.post url, data
In the handler in a Google app engine (python) backend, I read the value like so:
id_value = self.request.get('id')
LOG.info("%s", id_value)
It always print out '2' only.
How can I get the backend to obtain the list [2,3,4]?
$.post by default sends data in url-encoded format, which handles nested structures in its own way.
You might need to encode the data in JSON before sending and then decode it on the server side - an example is here.
The request object provides a get() method that returns values for arguments parsed from the query and from POST data.
If the argument appears more than once in a request, by default get()
returns the first occurrence. To get all occurrences of an argument
that might appear more than once as a list (possibly empty), give
get() the argument allow_multiple=True.
Hence you should use something like the below snippet. You can find more details here.
id_value = self.request.get('id', allow_multiple=True)
If you need to access variables url encoded in the body of a request (generally a POST form submitted using the application/x-www-form-urlencoded media type), you should use something like this.
id_value = self.request.POST.getall('id')

WebApi and Ampersands in name

So my angular website has a webapi with the following method.
[Route("items/{itemName}")]
public object GetMcguffinsByItem(string itemName)
{
return _mcguffinsService.GetAllByItemName(itemName);
}
However, an item name can have an ampersand as a valid character. However when attempting to use items that do have an ampersand, the method will return a 400 badrequest.
Im not sure how to go about fixing this problem.
For more verification: I was under the impression that encoding and using %26 is all required to pass an ampersand to part of the URI. It seems to be a common answer when searching my problem. I have excluded the angular as I can verify that it builds the string correctly, and other names produce the desired result.
The javascript method encodeURIComponent() followed by using the angular service double encodes the item name, and returns a 404.
EDIT:
Sample Input:
A&B 266
After Encoding:
A%26B%20266
Console:
angular.js:10722 GET http://localhost:60894/api/v1/mcguffins/items/A%26B%20266 404 (Not Found)
Using the browser on api directly with same input gives this error:
[HttpException (0x80004005): A potentially dangerous Request.Path value was detected from the client (&).]
System.Web.HttpRequest.ValidateInputIfRequiredByConfig() +11944671
System.Web.PipelineStepManager.ValidateHelper(HttpContext context) +55

Resources