Home > Web Front-end > JS Tutorial > body text

Cross-domain requests using jQuery's ajax jsonp to solve confusion_jquery

WBOY
Release: 2016-05-16 18:01:09
Original
888 people have browsed it

Directly executing the error method prompts an error - ajax jsonp has not been used before, and the understanding of it is similar to that of ordinary ajax requests, and there is no in-depth understanding of it; this error occurs, after several debugging (check the background code and js part Property settings) still doesn't work, which makes me feel very surprised and puzzled. Therefore, I decided to carefully study the use of ajax jsonp, and share the learning experience of the final successful test with everyone!
First, post the code that can be successfully executed:
(page part)

Copy code The code is as follows:




Untitled Page







(handler part)
Copy code The code is as follows:

<%@ WebHandler Language="C#" Class= "ajax" %>
using System;
using System.Web;
public class ajax : IHttpHandler {
public void ProcessRequest (HttpContext context) {
context.Response.ContentType = " text/plain";
string callbackFunName = context.Request["callbackparam"];
context.Response.Write(callbackFunName "([ { name:"John"} ] )");
}
public bool IsReusable {
get {
return false;
}
}
}

(request packet capture screenshot)

Ajax request parameter description:
dataType String
The data type expected to be returned by the server. If not specified, jQuery will automatically make intelligent judgments based on the MIME information of the HTTP package. For example, the XML MIME type is recognized as XML. In 1.4, JSON will generate a JavaScript object, and script will execute the script. The data returned by the server will then be parsed based on this value and passed to the callback function. Available values:
"xml": Returns an XML document that can be processed with jQuery.
"html": Returns plain text HTML information; the included script tag will be executed when inserted into the dom.
"script": Returns plain text JavaScript code. Results are not cached automatically. Unless the "cache" parameter is set. '''Note:''''When making remote requests (not under the same domain), all POST requests will be converted into GET requests. (Because the DOM script tag will be used to load)
"json": Returns JSON data.
"jsonp": JSONP format. When calling a function using JSONP format, such as "myurl?callback=?" jQuery will automatically replace ? with the correct function name to execute the callback function.
"text": Returns a plain text string
jsonp String
Rewrites the name of the callback function in a jsonp request. This value is used to replace the "callback" part of the URL parameter in a GET or POST request such as "callback=?". For example, {jsonp:'onJsonPLoad'} will cause "onJsonPLoad=?" to be passed to the server.
jsonpCallback String
Specify a callback function name for jsonp request. This value will be used instead of the random function name automatically generated by jQuery. This is mainly used to allow jQuery to generate unique function names so that it is easier to manage requests and provide callback functions and error handling. You can also specify this callback function name when you want the browser to cache GET requests.
The main difference between ajax jsonp and ordinary ajax requests is the processing of request response results. The response result shown in the above code is:
success_jsonpCallback([ { name: "John"} ] ); ——In fact, it is to call the jsonp callback function success_jsonpCallback and pass the string or json to be responded to here Method (as parameter value), its underlying implementation, a rough guess should be:

Copy code The code is as follows:

function success_jsonpCallback(data)
{
success(data);
}

After testing, ajax jsonp has no impact on synchronous or asynchronous requests. Okay, if there is anything wrong or incomplete in the explanation, I hope you will forgive me. You are welcome to leave a message and throw bricks!
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template