如何在AngularJS中通过Kendo Grid实现JSONP绕过CORS限制?
Got it, let's get your Kendo Grid set up to use JSONP instead of regular JSON to bypass that CORS issue—since you can't tweak the API server, this is the right workaround. Here's how to adjust your kendo.data.DataSource transport configuration step by step:
1. Update the Data Type and Enable JSONP
First, change the dataType from json to jsonp, and add the jsonp: true flag to tell Kendo to handle the JSONP callback mechanics. Also, note that JSONP works with GET requests only, so you can remove the contentType setting (since it's irrelevant for script-based JSONP calls):
transport: { read: { dataType: 'jsonp', // Changed from 'json' jsonp: true, // Enables Kendo's built-in JSONP handling // Optional: If your API expects a specific callback parameter name (e.g., 'cb' instead of 'callback') // jsonpCallbackParameter: 'cb' } }
2. Verify API Compatibility
Make sure the target API actually supports JSONP—this means it should accept a callback parameter (usually named callback by default) and return a response wrapped in that callback function. For example, a valid JSONP response looks like this:
callback_12345({ "id": 1, "name": "Sample Data" })
If your API uses a different parameter name (like jsonp or cb), use the jsonpCallbackParameter option to match it, as shown in the comment above.
3. Troubleshooting Quick Checks
- Pop open your browser's Network tab: You should see a GET request with a
callback(or custom) parameter appended to the URL, and the response should be a JavaScript function call wrapping your JSON data. - If errors persist, double-check that the API doesn't restrict callback origins (some servers whitelist specific domains for JSONP access).
That should resolve the CORS issue for your Kendo Grid setup!
内容的提问来源于stack exchange,提问作者Eddie

